vue2学习笔记1

版权声明:本文为博主原创文章,未经博主允许不得转载!!! https://blog.csdn.net/qq_19107011/article/details/79661867

前言

看到一些后台管理系统自己实现不出来感觉有点尴尬。
所以就恶补了一顿前端方面的知识。
选了个vue框架进行学习。
下面记录一些学习思路

思路

1.学习Vue对象的属性和方法
2.学习双向绑定
3.学习模板的使用和数据的传递(prop)
4.学习生命周期,推荐文章 Vue2.0 探索之路——生命周期和钩子函数的一些理解
5.v-bind和v-on的使用
6.使用计算属性和监听器
7.钩子函数
8.路由

其他

ps:代码来着互联网,仅作为学习交流使用

vue的模板渲染

<html>
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>

        <meta charset="utf-8"/>
        <div id="app">
            <my-component class="a b"></my-component>

        </div>

        <script type="text/javascript">
        Vue.component('my-component', {
          template: '<p class="foo bar">Hi</p>'
        })
         new Vue({
          el: '#app'
        })

          </script>
</html>

这里就是简单的一个模板渲染的效果,可是之前怎么也执行不成功,现在知道他要放在Vue这个一个根实例之前(上面)

子组件向父组件传递数据,下面这个例子是从菜鸟网上弄过来的,写得非常好!
基本思路就是,在子组件注册一个父组件的事件,然后内部激活他
具体效果可以参考这里子向父传递数据

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <div id="counter-event-example">
      <p>{{ total }}</p>
      <!--increment事件被激活,调用父组件的回调方法incrementTotal-->
      <button-counter v-on:increment="incrementTotal"></button-counter>
      <button-counter v-on:increment="incrementTotal"></button-counter>
    </div>
</div>

<script>
Vue.component('button-counter', {
  //子组件声明一个点击事件,改点击事件触发子组件的incrementHandler方法
  template: '<button v-on:click="incrementHandler">{{ counter }}</button>',
  data: function () {
    return {
      counter: 0
    }
  },
  methods: {
    //incrementHandler方法被调用,使用事件机制,激活increment事件
    incrementHandler: function () {
      this.counter += 1
      this.$emit('increment');//这里可以添加需要向父组件传递的数据
    }
  },
})
new Vue({
  el: '#counter-event-example',
  data: {
    total: 0
  },
    //父组件声明一个回调方法
    incrementTotal: function () {
      this.total += 1
    }
  }
})
</script>
</body>
</html>

指令的使用

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <p>页面载入时,input 元素自动获取焦点:</p>
    <input v-focus>
</div>

<script>
// 注册一个全局自定义指令 v-focus
Vue.directive('focus', {
  // 当绑定元素插入到 DOM 中。
  inserted: function (el) {
    // 聚焦元素
    el.focus()
  }
})
// 创建根实例
new Vue({
  el: '#app'
})
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_19107011/article/details/79661867