1.直接从store实例取值
export default {
computed: {
testNum() {
return this.$store.state.testNum;
}
}
};
2.使用mapState取值的多种方法
import { mapState } from "vuex";
export default {
data() {
return { localNum: 1 };
},
computed: {
...mapState({
testNum1: state => state.testNum1,
testNum2: "testNum2",
testNum3(state) {
return state.testNum1 + this.localNum;
}
}),
...mapState([
"testNum3"
])
}
};
3.使用module中的state
import { mapState } from "vuex";
export default {
computed: {
...mapState({
testNum1: state => state.moduleA.testNum1
}),
...mapState("moduleA", {
testNum2: state => state.testNum2,
testNum3: "testNum3"
}),
...mapState("moduleA",[
"testNum4"
])
}
};