標籤:
這兩天學了vuejs的模板,於此紀錄一下。
這是整個大檔案夾的配置,現在我們看到的所有檔案都不需要去管,說要關注也只需要關注“index.html”
"index.html"裡面是這樣的:
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>Questionnaire</title> <link href="http://cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"> </head> <body> <app></app> <!-- built files will be auto injected --> </body></html>
裡面的標籤<app>就是我們整個頁面的component,vuejs將頁面的各個部分作為component搭建起來,整個頁面就是一個大的component。
那麼app在哪兒呢?在src檔案夾下:有一個叫做App.vue的檔案,就是存放名為app的vue執行個體的地方。
如果需要在一個檔案中註冊多個Vue對象,可以在js中如下定義(vuejs官網代碼,局部註冊):
var Child = Vue.extend({ /* ... */ }) // 定義一個名為Child的組件var Parent = Vue.extend({ template: ‘...‘, components: { // <my-component> 只能用在父組件模板內 ‘my-component‘: Child // 這是一種內部的方式,表示Parent組件中包含Child,在Parent組件中直接使用<my-component></my-component>標籤即可使用Child組件 }})
回到App.vue,看一下裡面的script代碼:
import Header from ‘./components/Header‘import Index from ‘./components/Index‘import store from ‘./vuex/store‘export default { components: { ‘v-header‘: Header, Index }, store}
1. import語句:聲明其使用到的component或者vuex(全域資料容器,我是這樣理解的,後面講)
2. export語句: 匯出其組成,components包括Header和Index,而資料容器為store。
App.vue的HTML代碼如下:
<template> <div id="app"> <v-header></v-header> <router-view keep-alive></router-view> </div></template>
router-view是一個頂級的路由外鏈,其會渲染一個和頂級路由匹配的組件,也就是router.map裡面所聲明的組件,我們先看看router.js:
router.map({ ‘/index‘: { component: function(resolve) { require([‘./components/Index‘], resolve) } }, ‘/create‘: { component: function(resolve) { require([‘./components/Create‘], resolve) } }, ‘/edit/:questId‘: { component: function(resolve) { require([‘./components/Edit‘], resolve) } }, ‘/preview/:questId‘: { component: function(resolve) { require([‘./components/Preview‘], resolve) } }, ‘/data/:questId‘: { component: function(resolve) { require([‘./components/Data‘], resolve) } } })
比如說,我們在某個function中有一條語句:
this.$router.go(‘/components/Index‘);
那麼Index這個組件就會在App.vue中進行渲染,也就是該組件會在當前頁面中顯示。
可以看到最後兩個router看起來有點特別:
‘/preview/:questId‘:{...}
其實這是一種動態路徑的渲染,比如在一個表單管理系統,有很多表單,每個表單都有一個id,那麼開啟不同的表單時,就會有不同的questID,這時就需要動態路徑。那麼對這種動態路徑的捕獲則採用:
this.$router.go(`/preview/${this.questionnaire.id}`) // 在src/create.vue中的vuex下的actions -> publish中
在App.vue中,還有“store”這樣一條語句,它表示的是vuex目錄下的store.js:
store.js中儲存的是整個表單系統的資料,也就是一個頂級的資料庫,所有關於表單的更新都必須反饋到這個檔案中,那麼這是一種vuex的執行個體。
直接在js檔案中寫如下語句就可以產生一個vuex執行個體:
Vue.use(Vuex)
在那裡面會有:初始表單資料,以及對錶單資料進行更新的方法,比如:
constant KEY = ‘questionaireList‘let questionaire01 = {name : value}let questionaire02 = {name : value}let quesitonaire03 = {name : value}const mutations = {Method1, Method2 ...}
const state = {
questionaireList: JSON.parse(localStorage.getItem(KEY)), //所有的調查問卷,用KEY來檢索
currentQuestionaire: null // 當前正在操作的調查問卷
}
function updateStorage(state){
// 每當questionaire(state)有更新時,就更新本機存放區localStorage
localStorage.setItem(KEY, JSON.stringfy(state.questionaireList))
}export default new Vuex.Store({ state, mutations })
在這裡,存放了三個原始表單,定義了狀態更改mutations裡面的方法,定義了分發mutations的函數methodx
現在,我們可以通過store.state來讀取state對象,或者通過dispatch某mutation名字來出髮狀態state更改:
store.dispatch(‘Method1‘);
Vuejs使用筆記 --- 架構