这篇文章主要介绍vue.js安装vuex的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
vue.js安装vuex的方法:首先进入项目路径后,在项目终端输入相关代码;然后在项目的package json文件中显示vuex插件;接着在项目src目录中新建store js;最后在【main.js】中应用vuex即可。
vue.js安装vuex的方法:
1、安装:
进入项目路径后,在项目终端输入:npm install vuex --save
或者 cnpm install vuex --save
2、安装完成后,会在项目的package.json文件中显示vuex插件,如下:
"dependencies": {
"vue": "^2.5.2",
"vue-router": "^3.0.1",
"vuex": "^3.3.0"
},
3、再项目src目录中新建store.js,文件内容如下:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
export default new Vuex.Store({
//所有的数据都放在state中
state:{},
//操作数据,唯一的通道是mutations
mutations:{},
//actions,可以来做异步操作,然后提交给mutations,而后再对state(数据)进行操作
actions:{}
})
4、在main.js中应用vuex:
导入store,然后再new Vue中使用,代码如下:
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store' //导入store
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store, //在new Vue中使用
components: { App },
template: '<App/>'
})
以上是“vue.js安装vuex的方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。