更新
如果它在块范围内,则不能将任何值传递给外部 - 您需要从公共位置获取它或设置任何公共值
如我所见,var mixer = mixitup(allcards); 最终充当function,它通过传递给它的allcards 执行一些操作,然后返回一个值。
1 - 如果 mixitup 完全独立并且不使用组件使用的任何 vue 道具,则将其放置到不同的 helper 文件中
在你的helper.js
const mixitup = cards => {
// Do some operation with cards
let modifiedCards = 'Hey I get returned by your function'
return modifiedCards
}
export default {
mixitup
}
然后在您的vue 文件中只需import 并将其用作method。
在yourVue.vue
import Helpers from '...path../helpers'
const mixitup = Helpers.mixitup
export default {
name: 'YourVue',
data: ...,
computed: ...,
mounted() {
const mixer = mixitup(allcards)
},
methods: {
mixitup, // this will make it as `vue` method and accessible through
this
getCatval() {
var category = event.target.value;
this.mixitup(allcards)
}
}
}
2- 如果您的 mixitup 依赖于您的 vue 并且可以访问 vue 属性,则将其用作 mixins
在你的yourVueMixins.js:
export default {
methods: {
mixitup(cards) {
// Do some operation with cards
let modifiedCards = 'Hey I get returned by your function'
return modifiedCards
}
}
}
还有import 在你的vue 文件中:
import YourVueMixins from '...mixins../YourVueMixins'
const mixitup = Helpers.mixitup
export default {
name: 'YourVue',
mixins: [YourVueMixins] // this will have that function as vue property
data: ...,
computed: ...,
mounted() {
const mixer = this.mixitup(allcards)
},
methods: {
getCatval() {
var category = event.target.value;
this.mixitup(allcards)
}
}
}