vue.js安装vuex的方法:首先进入项目路径后,在项目终端输入相关代码;然后在项目的package json文件中显示vuex插件;接着在项目src目录中新建store js;最后在【main.js】中应用vuex即可。
本教程操作环境:windows7系统、Vue2.9.6版,该方法适用于所有品牌电脑。
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/>' })
相关免费学习推荐:javascript(视频)
以上就是vue.js如何安装vuex的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号