记录 Node 和 Vue 一些使用方法
安装 Node
安装 nvm 工具
1 | curl https:# raw.githubusercontent.com/creationix/nvm/master/install.sh | bash |
通过 nvm 安装 Node, npm
1 | nvm install 16.15.0 |
搭建 Vue
安装 vue-cli 脚手架
1 | npm install -g @vue/cli |
安装 vue-router
1 | npm install -g vue-router@3 |
1 | // main.js |
安装 axios
1 | npm install -g axios |
1 | import axios from 'axios' |
删除 node_modules
1 | npm install -g remove-node-modules |
安装 Vuex
1 | npm install -g vuex@3 |
配置 Vuex
单独创建一个
states.js
,编辑 vuex 相关的配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// states.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{
},
mutations:{
},
actions:{
}
})main.js
中导入第一步的配置文件,并在 vue 实例中声明一下1
2
3
4
5
6
7
8
9import store from "./states.js"
new Vue({
// 使用 router
router,
// 使用 vuex
store,
render: h => h(App),
}).$mount('#app')
使用 Vuex
state
的使用 (当成组件中的data)1
2
3
4
5
6
7
8state: {
products: [
{name: '鼠标', price: 20},
{name: '键盘', price: 40},
{name: '耳机', price: 60},
{name: '显示屏', price: 80}
]
}getters
相当于之前实例中讲过的computed
计算属性1
2
3
4
5
6
7
8
9
10
11
12getters:{
// 获取数据的方法,类似于计算属性
saleProducts: (state) => {
let saleProducts = state.products.map((product) => {
return {
name: product.name,
price: product.price / 2
}
})
return saleProducts;
}
}mutations
相当于methods
,提供修改数据的方法1
2
3
4
5
6
7
8mutations: {
minusPrice(state, payload) {
let newPrice = state.products.forEach(product => {
// 将数据的price价钱,减payload
product.price -= payload
})
}
}actions
相当于异步的methods
,支持一步操作,注意actions
还是要通过mutations
修改数据1
2
3
4
5
6
7
8
9actions: {
minusPriceAsync(context, payload ) {
// setTimeout定时,延迟2000毫秒,回调第一个参数的函数
setTimeout(() => {
// context提交,调用mutations中的minusPrice函数
context.commit( 'minusPrice', payload );
}, 2000)
}
}
搭建 Electron-Vue
安装 electron-vue 脚手架模板
1 | npm install -g @vue/cli |
解决出现的报错
libgconf 问题
1
sudo apt-get install libgconf-2-4
控制台 Object.formEntries
问题
1
npm i -g polyfill-object.fromentries
1
2// dev-client.js
import 'polyfill-object.fromentries';
404 not found
问题
1
2
3// dev-runner.js
// 69th row,解开注释
app.use(hotMiddleuse)