TracerLee / tracerlee.github.io

Personal blog written by Tracer
4 stars 0 forks source link

MVVM记录 #19

Open TracerLee opened 7 years ago

TracerLee commented 7 years ago

学习MVVM框架

TracerLee commented 7 years ago

vue.js

此记录由于为1.x的版本不再作为学习参考 2017年4月7日 00:54:07

摘录

image

组件的应用模板

<div id="app">
  <app-nav></app-nav>
  <app-view>
    <app-sidebar></app-sidebar>
    <app-content></app-content>
  </app-view>
</div>
var vm = new Vue({
  // 选项
})

img

阅读到http://cn.vuejs.org/guide/class-and-style.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>vue demo</title>
        <script src="./node_modules/vue/dist/vue-1.0.js"></script>
    </head>
    <body>
        <div id="example-2">
            <p v-if="greeting">Hello {{* name}}!</p>
            <p v-if="greeting">Hello {{name}}!</p>
            <p>{{{html}}}</p>
            <p>{{ok?'yes':'no'}}</p>
            <p>{{ message.split('').reverse().join('') }}</p>
            <p>首字母大写“过滤器”: {{ message | capitalize }}</p>
            <a :href="http://baidu.com">link</a>
            <button :disabled="disabledFn">Button</button>
            <button @click="clickFn">Button</button>
            <div>a={{a}},b={{b}}</div>
            <div>fullName: {{fullName}}; firstName: {{firstName}}; lastName: {{lastName}}</div>
        </div>
    </body>
</html>

<script>
var vm = new Vue({
    el: "#example-2",
    data: {
        greeting: 1,
        name: 'Vue.js',
        html: '<strong>输出html</strong>',
        ok: false,
        message: 'hello world',
        a: 100,
        firstName: 'Foo',
        lastName: 'Bar'
    },
    created: function () {
        console.warn('vm is created.');
    },
    methods: {
        disabled: function () {
            console.error('disabled');
        },
        clickFn: function () {
            alert('点击');
        }
    },
    computed: {
        b: function () {
            return this.a + 1
        },
        // 相当于getter
        // fullName: function () {
        //  return this.firstName + ' ' + this.lastName
        // }
        fullName: {
            // getter
            get: function () {
                return this.firstName + ' ' + this.lastName
            },

            // setter
            set: function (newVal) {
                var names = newVal.split(' ');
                this.firstName = names[0];
                this.lastName = names[names.length - 1];
            }
        }
    }
});

vm.$watch('name', function (newVal, oldVal) {
    console.log('变化了',newVal,oldVal);
});
</script>
TracerLee commented 7 years ago

vuex map的使用遇到的坑

vuex的map有如下写法

import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
  // 使用对象展开运算符将 getters 混入 computed 对象中
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}

其中的使用扩展符...来进行对象展开运算,标准的ES6只对数组有效,故一直报错...

查明原因原来是Babel的不同等级支持度不一样,需要设置不同的stage

解决方案

# 安装babel-preset-stage-2
$ npm i babel-preset-stage-2 -D
// 设置.babelrc,新增了stage-2
{
  "presets": [
    ["es2015", { "modules": false }],
    "stage-2"
  ]
}

再次启动程序npm run dev,不报错了

汗!真是折腾,官网vuex没有告知这一点

参考: 如何区分Babel中的stage-0,stage-1,stage-2以及stage-3(一)

TracerLee commented 7 years ago

重新看Vue.js教程得到的启示

生态学习

基础