1.如何在1中使用vuex?Nuxt?Nuxt.js内置了对vuex模块的引用,所以不需要额外的安装。
Nuxt.js将尝试在应用程序根目录下找到存储目录。如果这个目录存在,它将做以下事情:1.1引用vuex模块1.2向厂商添加vuex模块构建配置1.3设置Vue根实例的存储配置项。
Nuxt.js支持两种使用store的方式:普通方式:store/index.js返回一个Vuex。存储实例模块:每。store目录中的js文件将被转换成一个由状态树命名的子模块(当然,index是根模块)
2.如何在2中模块化vuex?Nuxt?例如2.1-将index.js设置为根模块,将child1.js和child2.js设置为两个子模块
2.2无需返回Vuex。Store实例在store/index.js中,但是状态、变化和动作可以直接暴露出来:(以下是一个例子:存储在:index中的商品总价、存储在child1中的单价和存储在child2中的数量)
export const state=()=({ TotalPrice :0,});export const variations={ total price(state){//total price//state . total price=state . num * state . price错误模式:使用子模块的状态,文件名应加在变量名之前,如下所示:state . total price=state . child 1 . price * state.child2.num//correct模式};状态、变化和动作也直接显示在子模块中:
child 1 . js1 export const state=()=({ price :10,//单价});export const突变={ getPrice(state,price){ state . price=price } };child 2 . js2 export const state=()=({ num :5,});导出常量变量={getnum (state,num){//quantity state . num=num } };3.获取Vue文件中vuex的数据,在突变中调用方法修改数据script exportdefault { name : ' test ',data(){ return { total price : this。$ store.state.totalprice,//取值num3360this。$ store.state.child2.num在index.js(根模块)中,//取值price:this。$ store.state.child1.price in子模块,//取子模块中的值}},}/script methods : { setTotalPrice(){ this。$store.commit('totalPrice') },setNum(){ this。$store.commit('child2/getNum ',Parameters)//使用方法this。$ store.commit('文件名/方法名',参数)},setprice () {this。$ store.commit ('child1/getprice ',参数)//使用方法},},Supplement :使用子模块的动作:this。$ store.dispatch('文件名/变量名')参考链接:3359 www.jb51.net/article/169502.htm https://www.jb51.net/article/169504.htm.
以上就是本文的全部内容。希望对大家的学习有帮助,支持我们。