一、index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>vuex-note</title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
这个没什么说的,注意 div 的 ID 就行
二、main.js
import Vue from 'vue'
import App from './App'
import store from './store'
Vue.config.productionTip = false
new Vue({
el: '#app',
store,
components: { App },
template: '<App/>'
})
1.在 import 时什么时候需要 ' ./ '?
从项目模块中导出,引入时不需要 ./,而从自己写的组件中引入时需要 ./
2.什么时候需要 import {aaa} from abc 这种加大括号的引入?什么时候不需要?
当 abc 中被导出的部分是 export aaa 时
当 import 的是被 export default 导出的部分时不加 {},并且可以起个别名
3.项目结构中并没有 store 文件,只有 store 文件夹,那 import store from './store' 是什么意思?
不知道,求指教
4. new Vue 中单独的 store 是什么意思?
ES6 的一种简写方式,缩写之前是 store:store,这句话的意思是为全局注入 Vuex,这样在各个组件中都可以通过 this.$store 去调用状态库,如果不在全局注入,则需要在每个组件中单独引入,多了会很麻烦
三、store 下的 index.js
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import mutations from './mutations'
import actions from './actions'
Vue.use(Vuex)
const defaultNote = {
id: +new Date(),
title: '新建笔记' + new Date().getMilliseconds(), // 加时间是为了做一下区分
content: '笔记内容',
fav: false
}
// 可以理解为一个状态的仓库
const state = {
notes: [defaultNote], // 以数组方式存放所有的笔记
activeNote: defaultNote, // 用来记录当前笔记
show: 'all' // 用于切换 全部 / 已收藏 两种不同列表的标识
}
export default new Vuex.Store({
state,
getters,
mutations,
actions
})
内容版权声明:除非注明,否则皆为本站原创文章。

