iT邦幫忙

0

使用 eval 發生的怪事 vue

  • 分享至 

  • twitterImage

框架使用 vue
這個可以正常運行

return eval(`this.$store.state.${k}`)

但是當這樣時卻會說 common is not defined"

在 template 使用時

handleLabel('label.endTime')

資料

const common = {
    label: {
        endTime: '結束時間'
    }
}

export default common
import common from '@/common' // 確定本身沒問題,其他 func 可使用

export default {
    // ...
    methods: {
        function handleLabel(k) {
            return eval(`common.${k}`)
        }
    }
}

這是咋了?
確定值帶進去的都是對的!

fillano iT邦超人 1 級 ‧ 2020-08-06 17:39:10 檢舉
你怎麼不用 return common[k] ?
火爆浪子 iT邦研究生 1 級 ‧ 2020-08-07 09:36:54 檢舉
有試過這個但不行啊。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
fillano
iT邦超人 1 級 ‧ 2020-08-07 15:14:44
最佳解答

我試了好像沒問題...不然你這樣改試試看:

handleLabel: function (k) {
    return k.split('.').reduce((a, c) => a[c], common);
}
火爆浪子 iT邦研究生 1 級 ‧ 2020-08-07 15:51:16 檢舉

有這種事⋯⋯好我來試試

火爆浪子 iT邦研究生 1 級 ‧ 2020-08-07 16:46:25 檢舉

這個反而可以⋯⋯

我要發表回答

立即登入回答