为了账号安全,请及时绑定邮箱和手机立即绑定

请问怎么正确使用Vue.js的组件

请问怎么正确使用Vue.js的组件

江户川乱折腾 2019-07-14 12:08:15
请问怎么正确使用Vue.js的组件
查看完整描述

2 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

  1. # 下载最新的vue$ npm install vue

  2. js 引用 vue.js

  3. 开始代码,感受vue强大的双向数据绑定

1

2

3

4

5

6

7

8

9

10

11

<div id="app">

  <p>{{ message }}</p>

  <input v-model="message">

</div>

     

new Vue({

  el: '#app',

  data: {

    message: 'Hello Vue.js!'

  }

})

实战代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<div id="app">

  <input v-model="newTodo" v-on:keyup.enter="addTodo">

  <ul>

    <li v-for="todo in todos">

      <span>{{ todo.text }}</span>

      <button v-on:click="removeTodo($index)">X</button>

    </li>

  </ul>

</div>

     

new Vue({

  el: '#app',

  data: {

    newTodo: '',

    todos: [

      { text: 'Add some todos' }

    ]

  },

  methods: {

    addTodo: function () {

      var text = this.newTodo.trim()

      if (text) {

        this.todos.push({ text: text })

        this.newTodo = ''

      }

    },

    removeTodo: function (index) {

      this.todos.splice(index, 1)

    }

  }

})

Vue整个生命周期示意图:



 



查看完整回答
反对 回复 2019-07-15
?
GCT1015

TA贡献1827条经验 获得超4个赞

var vue = new Vue({

el: 'body',
data: {
//这里负责数据
myData: {},
},
ready: function () {
//这里是vue初始化完成后执行的函数
this.test();
},
methods: {
//这里是自定义的方法
test: function () {
alert('test');
}
}

});

 



查看完整回答
反对 回复 2019-07-15
  • 2 回答
  • 0 关注
  • 422 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信