手把手教你Vue框架从入门到实战项目(二)

第二章 Vue基础精讲

Vue实例

在Vue中,所有的组件都可以理解为Vue实例。一个Vue应用通过new Vue创建的根Vue实例。

<!-- 文本插值{
   {message}} -->
<div id="app">{
  {message}}</div>
//全局组件
Vue.component('item',{
   
    template:'<div>你好</div>'
})
//实例化一个Vue
var vm = new Vue({
   
    el: "#app",
    data:{
   
        message:"hello world"
    }
})

当一个Vue实例被创建时,Vue实例中的el属性(element)就是获取视图中的元素,相当于document.getElementById('app')的作用。另外它将data对象中的所有属性加入到Vue的响应式系统中。也就是说当属性的值发生改变时,视图中也会改变。反知,当视图中的元素值改变时,data中的属性相应的值也会改变。

当然如果你想阻止修改现有的属性,可以使用Object.freeze()方法来达到响应系统无法再追踪到变化。

<div id="app">
    <p>{
  {content}}</p>
    <button v-on:click="changebtn">修改</button>
</div>
var obj={
   
    content:'asdas'
}

Object.freeze(obj);
let app=new Vue({
   
    el: '#app',
    data:obj,
    methods:{
   
        changebtn:function(){
   
            this.content='1111'
        }
    }
})

除了数据属性,Vue实例还暴露了一些有用的实例属性与方法。它们都有$前缀,以便与用户定义的属性区分开来。

//实例化一个Vue
var vm = new Vue({
   
    el: "#app",
    data:{
   
        message:"hello world"
    }
})

vm.$el === document.getElementById('app');//true
console.log(vm.$data.message);//"hello world"

//$watch是一个实例方法 直译:观察
//第一个参数表示要观察的属性,第二个是个回调函数,函数的有两个参数,分别是new,old的值
vm.$watch('message',function(newVal,oldVal){
   
    console.log(newVal,oldVal);//newVal hello world
})
vm.$data.message='newVal';
实例生命周期钩子

什么是生命周期函数(钩子)?生命周期函数就是Vue实例在某一个时间点会自动执行的函数。

生命周期函数,并不是写在methods对象属性中。

注意:不要在选项属性或回调上使用箭头函数,比如created:() => console.log("...")vm.$watch('a',newVal => this.myMethod())。因为箭头函数并没有thisthis会作为变量一直向上级词法作用域查找,直至找到为止,经常导致Uncaught TypeError: Cannot read property of undefinedUncaught TypeError: this.myMethod is not a function之类的错误。

生命周期示意图
https://cn.vuejs.org/images/lifecycle.png?_sw-precache=b3251a15e5779fcfec925b78a149f5c8
var vm = new Vue({
   
    el:'#app',//执行完created函数后,会检查是否定义el属性,如果是否,当调用vm.$mount('#app')又会进行下一步
    template:"<p>{
   {message}}</p>",//如果没有定义template属性,vue会将html中的元素作为渲染模板
    data:{
   
        message:'你好'
    },
    beforeCreate:function(){
   
    	console.log('beforeCreate');//beforeCreate
	},
    created:function(){
   
        console.log('created');//created
    },
    beforeMount:function(){
   
        console.log('beforeMount');//beforeMount
        console.log(this.$el);//<div id="app"></div>  没有渲染之前什么都没有
    },
    mounted:function(){
   
        console.log('mounted');//mounted
        console.log(this.$el);//<div>hello world</div>
    },
    beforeUpdate:function(){
   
        console.log('beforeUpdate');//beforeUpdate
        console.log(this.message);//更改后的值
    },
    updated:function(){
   
        console.log('Updated');//Updated
        console.log(this.message);//更改后的值
    },
    //当调用vm.$destroy()时,下面的就会执行
    beforeDestroy:function(){
   
        console.log('beforeDestory');
    },
    destroyed:function(){
   
        console.log('destroyed');
    }
})

模板语法

文本插值

常用的数据绑定形式是使用“Mustache”语法 (双大括号) 的文本插值。

<span>{
  {message}}</span>

也可以使用v-text指令进行文本插值。

<span v-text="message"></span>

这里再介绍一个v-once指令,使用这个指令后,插值处的内容不会因为data数据的改变而改变。

<span v-once>{
  {message}}</span>

如果我们要实现像原生JS中innerHTML一样的功能,要怎么实现呢?这里我们使用的是v-html指令。

<span v-html="myhtml" id="app"></span>
new Vue({
   
    el:'#app',
    data:{
   
        myhtml:'<span style="color:red">我是红色</span>'
    }
})

值得一提的是,在“Mustache”语法、v-textv-html中的内容不止可以使用变量,同样也可以使用JS表达式。

<span>{
  {message + 'vue'}}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋承佑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值