
说明
这个是根组件,具有一般组件的基本结构,同时在编译结束的时候会把根组件渲染到#app,这里的#app其实是指的index.html里面的#app。

编译过程
app.vue->main.js->index.html
编译app.vue->编译完进行代码替换->替换index.html里面的#app


computed: {
now: function () {
return Date.now()
}
}
说明
参数
代码示例

watch:{
syncmsg:{//监听syncmsg,设置了immediate属性
handler(newval,oldval){
console.log("newval",newval)
console.log("oldval",oldval)
},
deep:true,
immediate:true
}
},

效果
依次点击更新父组件、修改子组件内容按钮(这里知识点见6(6).sync和emit)

<template>
<div class="demo-bind-box">
{{bindmsg}}
div>
template>
<script>
export default {
props:['bindmsg'],
data(){
return{
//bindmsg:'hello'
}
}
}
script>
<bindBox :bindmsg="mybindmsg">bindBox>


在上面的基础上对style属性进行绑定,绑定了文字的颜色和大小
<bindBox :bindmsg="mybindmsg" :style="'color:red;font-size:36px;'">bindBox>

也可以绑定的值是一个对象(蓝色是绑定的值是对象的效果)
<bindBox :bindmsg="mybindmsg" :style="{color:'blue','font-size':'36px'}">bindBox>

(3.1)v-bind 默认绑定到 DOM 节点的 attribute 上,使用 .prop 修饰符后,会绑定到 property,这里要明确HTML attribute和DOM property的区别:
(3.2)直接把div标签当作对象,用“.”输出即是property属性,“.”是不能输出自定义绑定的属性的。
<bindBox id="mybindbox" :bindmsg="mybindmsg" :bindprop.prop="mybindmsg" :bindattr="mybindmsg" :style="'color:red;font-size:36px;'">bindBox>
(3.3)使用.prop修饰符绑定属性property-bindprop

(3.4)直接使用v-bind是将属性绑定到attribute-bindattr
let box=document.getElementById('mybindbox').attributes
console.log("box:",box)

注
如果要输出两个绑定的属性,前者(绑定到attribute)需要调用 getAttribute(“bindmsg”) 方法,后者(绑定到 property)可以 直接使用".bindprop" 获取
文档
https://cn.vuejs.org/v2/guide/forms.html#在组件上使用-v-model
说明
你可以用 v-model 指令在表单 、
示例
<input v-model="message" placeholder="edit me">
<p>输入框内容为: {{ message }}p>

修饰符
代码
<input v-model="message" placeholder="edit me">
<p v-show="message=='true'">输入框内容为: {{ message }}p>
v-show=“false”


v-show=“true”


只有当输入的值为true的时候才会出现文本
v-if=“false”

v-show=“true”

搭配使用v-else
<input v-model="message" placeholder="edit me">
<p v-if="message=='true'">输入框内容为: {{ message }}p>
<p v-else style="color:red">输入框内容为: {{ message }}p>
fruits:[{
name:'apple',
price:10
},{
name:'orange',
price:11
},{
name:'peach',
price:12
}]
<div v-for="item in fruits">
<div>{{item.name+":"+item.price}}div>
div>

<template>
<div class="demo-bind-box" :bindattr="mybindattr" >
{{bindmsg}}
<br>
子组件:{{emitmsg}}
<br>
<button @click="emitMsg">更新父组件syncmsg内容button>
<br>
div>
template>
<script>
export default {
props:['emitmsg'],
data(){
return{
bindmsg:'hello',
mybindattr:'attr',
}
},
methods:{
emitMsg(){
this.$emit('update:emitmsg','更新父组件内容')
}
}
}
script>
①初始时:外部组件调用
<bindBox :emitmsg.sync="syncmsg">bindBox>
<div>父组件:{{syncmsg}}div>
<button @click="changeChildMsg">父组件修改子组件内容button>

②子组件更新父组件:点击更新父组件,调用emit方法动态更新emitmsg
emitMsg(){
this.$emit('update:emitmsg','更新父组件内容')
}

③父组件更新子组件:点击修改子组件内容,该按钮直接修改syncmsg,子组件中的emitsmg是同步更新的
<button @click="changeChildMsg">父组件修改子组件内容button>
changeChildMsg(){
this.syncmsg="父组件又更新了子组件的内容"
},
