美文网首页前端知识点
Vue.js学习笔记(一)

Vue.js学习笔记(一)

作者: AMONTOP | 来源:发表于2019-01-02 17:00 被阅读14次

●Vue实现双向绑定的原理:
利用了 Object.defineProperty() 这个方法重新定义了对象获取属性值(get)和设置属性值(set)的操作来实现的

var Book = {}
var name = '';
Object.defineProperty(Book, 'name', {
  set: function (value) {
    name = value;
    console.log('你取了一个书名叫做' + value);
  },
  get: function () {
    return '《' + name + '》'
  }
})
 
Book.name = 'vue权威指南';  // 你取了一个书名叫做vue权威指南
console.log(Book.name);  // 《vue权威指南》

●生命周期:


image.png

这么多钩子函数,我们怎么用呢?

beforecreate : 举个栗子:可以在这加个loading事件
created :在这结束loading,还做一些初始化,实现函数自执行
mounted : 在这发起后端请求,拿回数据,配合路由钩子做一些事情
beforeDestroy: 你确认删除XX吗? destroyed :当前组件已被删除,清空相关内容

首先根据https://www.jianshu.com/p/e168b0d9bda9构建一个初始项目,
运行:

npm run dev
  • 下面是基础示例
    App.vue:
<template>
  <div id="app">
    <ComputedMethod/>
  </div>
</template>

<script>
  import HelloWorld from "./components/HelloWorld"
  import ComputedMethod from "./components/ComputedMethod"
export default {
  name: 'app',
  components:{
    HelloWorld,
    ComputedMethod
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

ComputedMethod.vue:

<template>
  <div id="example">
    <p>Original message: "{{ message }}"</p>
    <p>Computed reversed message: "{{ reversedMessage }}"</p>
  </div>
</template>

<script>
    export default {
        name: "ComputedMethod",
      data(){
          return{
            message: "Hello"
          }
      },
      computed:{
          reversedMessage:function(){
            return this.message.split('').reverse().join('')
          }
      }
    }
</script>

<style scoped>

</style>

问题1:

Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.

解决方法:

vue模板只能有一个根对象

所以你想要出现正常的效果,你的用一个div来或是别的标签来包裹全部的元素

正确的写法就是:

<template>
    <div>
        <el-button type="primary">haha1</el-button>
        <div>hahhaa</div>
        <el-input type="text" placeholder="测试一下"></el-input>
        <h1>{{test1}}</h1>
    </div>
</template>
问题2:
export default {
    name: 'app',
    data:{
           name: ''
    }
}

出现The "data" option should be a function that returns a per-instance value in component definitions

原因:简单地说,在实例中data是对象, 在组件中data就得是函数返回对象
解决办法:将data改为函数且返回data中的数据对象

export default {
    name: 'app',
    data() {
        return {
           name: ''
        }
    }
}

相关文章

  • 前端基础知识学习---Vue.js学习(一)模板语法

    Vue.js学习笔记 Vue.js的使用之HelloWord 引入Vue.js 创建Vue对象其中el:指定根el...

  • Vue.js 学习笔记(一)

    声明:本文章并非原创,而是参考黑马程序员Vue.js教程配套资料,仅供学习使用,侵删。 Vue.js 学习笔记 什...

  • 初识vue.js

    vue.js官网教程学习笔记和学习摘要 起步 安装 一个简单的方法,直接把一个vue.js引入你的HTML页面中,...

  • 【个人提升】vuex构建单页应用

    前言:在最近学习 Vue.js 的时候,看到国外一篇讲述了如何使用 Vue.js 和 Vuex 来构建一个简单笔记...

  • vue.js源码学习笔记

    参考:vue.js官网Vue.js 源码学习笔记Vue2.0源代码阅读 文件结构梳理 整体目录 源代码实现目录 模...

  • vue学习笔记

    title: vue.js学习笔记(一)date: 2018-03-28 19:09:16tags: vue.js...

  • vue.js组件上

    前言 本文由阅读一篇vue.js组件文章学习后笔记http://www.cnblogs.com/keepfool/...

  • Vue.js 学习笔记(一)

    v-cloak、v-text 和 v-html 的使用 v-cloak 的使用 v-cloak 可以解决当网页很慢...

  • vue.js学习笔记一

    vue.js循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, s...

  • Vue.js学习笔记(一)

    ●Vue实现双向绑定的原理:利用了 Object.defineProperty() 这个方法重新定义了对象获取属性...

网友评论

    本文标题:Vue.js学习笔记(一)

    本文链接:https://www.haomeiwen.com/subject/tiazlqtx.html