使用vuex mapState时,文档说您可以使用如下所示的传播语法,我已经按预期进行了工作。
我只是对这实际上在做什么感到好奇,所以我对使用扩展语法有了更好的理解。
这会...
computed: {
...mapState([
'message',
'storeArray'
])
}
Run Code Online (Sandbox Code Playgroud)
有效地做到这一点...?
computed: {
message(){
return this.$store.state.message
}
storeArray(){
return this.$store.state.storeArray
}
}
Run Code Online (Sandbox Code Playgroud) 我意识到这更像是一个普遍问题,但我已经阅读了这里的类似答案,但我找不到更多概述。我是回调的新手,我试图了解何时应该使用它们。
MDN 网络文档有这个例子;
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
Run Code Online (Sandbox Code Playgroud)
然而,我很难看出这比下面的更有利,我没有将问候函数作为参数传递?
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput() {
var name = prompt('Please enter your name.');
greeting(name);
}
processUserInput();
Run Code Online (Sandbox Code Playgroud) 我正在练习将axios 与 Vue一起使用,但我认为这可能是一个更一般的 JSON 问题。
我已成功使用 axios 获取本地 products.json 文件,然后使用过滤器创建一个新数组,该数组仅包含具有匹配部门属性的产品,并将它们循环出去。
这是执行此操作的正确方法吗?或者我实际上可以过滤原始 axios 调用上的 JSON 结果吗?我知道我可以传递一个参数,该参数将依次执行特定的数据库调用,并且首先只提供所需的 JSON。
data(){
return {
products: []
}
},
components: {
Product
},
computed: {
foodProducts(){
return this.products.filter(x => x.department == 'Food')
}
},
mounted() {
axios
.get('./json/products.json')
.then(response => (this.products = response.data.products))
}
Run Code Online (Sandbox Code Playgroud)
谢谢。只是想澄清其背后的理论。