• Vuejs使用笔记 --- 框架


    这两天学了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中,打开一个指定id的表单页面
    

    在App.vue中,还有“store”这样一条语句,它表示的是vuex目录下的store.js:

    store.js中存储的是整个表单系统的数据,也就是一个顶级的数据库,所有关于表单的更新都必须反馈到这个文件中,那么这是一种vuex的实例。

    直接在js文件中写如下语句就可以生成一个vuex实例:

    import Vuex from 'vuex'
    import Vue from 'vue'
    Vue.use(Vuex)

    在那里面会有:初始表单数据,以及对表单数据进行更新的方法,比如:

    const 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的函数updateStorage

    现在,我们可以通过store.state来读取state对象,或者通过dispatch某mutation名字来出发状态state更改:

    store.dispatch('Method1'); 

     在外部的vue组件中,通常使用如下语法来访问store:

    vuex: {
            getters: {
                 questionnaireList: state => state.questionnaireList  // 读取表单数据
            },
            actions: {
                 setQuest({dispatch}, item) {
                    dispatch('SET_QUEST', item)
                },
                removeQuest({dispatch}, item) {
                    dispatch('RM_QUEST', item)
                }
            }
    }
    

      

    在这种层层嵌套的风格中,如何使得子组件访问父组件的属性呢?这里需要用到props函数:

    Vue.component('example', {  // 注册了一个组件
        props: ['showCalendar'] 
    
    })
    

    当其他的组件使用到这个example组件时,就会得到example组件的'showCalendar'属性。

    现在,我们已经知道了vue的框架是长什么样的,所以在使用vue的时候,我们有几件事情是需要关注的:

    1. 确定数据类型,如表单中必然包含标题、问题等

    2. 确定页面所需要的组件,比如需要一个通用的日历组件和标题组件。

    3. 确定整个工程的页面,如创建、编辑、删除等,都在不同的页面操作,这些以页面为单位的components都应该在router中进行注册,并且编写相关的方法来决定跳转的时机。

    4. 在每个component中设定相应的css样式等

    5. 如果需要和服务器进行数据交互还需要vue-resource,使用起来也很方便:

    {   // 可参考: https://github.com/vuejs/vue-resource/blob/master/docs/http.md
        // GET /someUrl
        this.$http.get('/someUrl').then((response) => {
          // success callback
        }, (response) => {
          // error callback
      });
    }

    下面我们来看一下每一个component内部是怎么实现的 >> component内部实现  

      

      

  • 相关阅读:
    阿里云ECS linux通过rinetd 端口转发来访问内网服务
    阿里云ECS linux通过iptables 配置SNAT代理网关,实现局域网上网
    适用于CentOS6.x系统的15项优化脚本
    ELK学习笔记
    MAC OSX环境下cordova+Ionic的安装配置
    Windows下 VM12虚拟机安装OS X 10.11 和VM TOOLS
    cordova 下载更新
    android adb常用命令
    ionic实现双击返回键退出功能
    ionic ngCordova插件安装
  • 原文地址:https://www.cnblogs.com/yyh1993/p/5846586.html
Copyright © 2020-2023  润新知