Vuejs使用筆記 --- 架構

來源:互聯網
上載者:User

標籤:

這兩天學了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使用筆記 --- 架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.