var app = new Vue({...})
var obj = {foo: '10'}Object.freeze(obj);new Vue({el: '#app',data: obj})
图
钩子函数参数
<div id="example"><p>Original message: "{{ message }}"</p><p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>
var vm = new Vue({el: '#example',data: {message: 'Hello'},computed: {// 计算属性的 getterreversedMessage: function () {// `this` 指向 vm 实例ssage.split('').reverse().join('')}}
})
插值
{{message}}
<p v-html="message"></p>
{{a+b+c}}
缩写
watch: {message: function(newValue, oldValue) {console.log("新" + newValue + "旧" + oldValue)}
}
//全局过滤器
Vue.filter('captitalize', function(value){...
})
//局部过滤器
falters: {capitalize: function(value) {...}
}
<div v-if="type === 'A'">A
</div>
<div v-else-if="type === 'B'">B
</div>
<div v-else-if="type === 'C'">C
</div>
<div v-else>Not A/B/C
</div>
<template v-if="loginType === 'username'"><label>Username</label><input placeholder="Enter your username" key="username-input">
</template>
<template v-else><label>Email</label><input placeholder="Enter your email address" key="email-input">
</template>
<ul id="example-2"><li v-for="(item, index) in items">{{ parentMessage }} - {{ index }} - {{ ssage }}</li>
</ul>
var example2 = new Vue({el: '#example-2',data: {parentMessage: 'Parent',items: [{ message: 'Foo' },{ message: 'Bar' }]}
})
<ul id="v-for-object" class="demo"><li v-for="value in object">{{ value }}</li>
</ul>
new Vue({el: '#v-for-object',data: {object: {title: 'How to do lists in Vue',author: 'Jane Doe',publishedAt: '2016-04-10'}}
})
<div id="app"><h3>没有报道的学生名单</h3><ul><li v-for="n in items" v-if="!n.value">{{n.name}}</li></ul>
</div>
<script>var app = new Vue({el: '#app',data: {items: [{name: '小明',},{name: '小红', value:'已报道'},{name: '小华', value: '已报道'},{name: '小思',},]}})
</script>
<div id="app"><input v-focus></input>
</div>
<script>
//全局指令
Vue.directive('focus', {inserted: function(el) {el.focus}
})
//局部指令
var app = new Vue({el: '#app',directives: {focus: {//指令。。。}}
})
</script>
<style>.style1 {color: white;}.style2 {color: black;}
</style>
<div id="app"><div v-bind:class="{ 'style1', 'style2' }">{{message}}</div>
</div>
<script>new Vue({el: '#app',data: {message: '数组语法'}})
</script>
<style>.static {color: #3a8ee6;}.style1 {color: white;}.style2 {color: black;}
</style>
<div id="app"><div class="static" v-bind:class="{style1: boole1, 'style': boole2}"></div>
</div>
<script>new Vue({el: '#app',data: {boole1: true,boole2: true}})
</script>
<div id="app"><div v-bind:style="{color:'red', fontSize:'30'}">对象语法</div>
</div>
<script>new Vue({el: '#app',})
</script>
<div id="app"><div v-bind:style="[styleObj1, styleObj2]">对象语法</div>
</div>
<script>new Vue({el: '#app',data: {styleObj1:{color: 'blue'}},computed: {styleObj2:function (){return {padding: '30px'}}}})
</script>
<div id="app"><div>单击:<button v-on:click="reduce(1)">-1岁</button><button v-on:click="add(1)">+1岁</button></div><p>老王的年龄{{age}}岁</p><div>双击:<button v-on:click="reduce(10)">-10岁</button><button v-on:click="add(10)">+10岁</button></div>
</div>
<script>new Vue({el: '#app',data: {age: 50},methods: {add:function (change){this.age-=change},reduce:function (change){this.age-=change},}})
</script>
<div @click.stop="inside">阻止事件</div>
<input ="submit">
<!-- Alt + C -->
<input @keyup.alt.67="clear"><!-- Ctrl + Click -->
<div l="doSomething">做一些操作</div>
<button l.exact="onClick">A</button>
v-model能绑定的元素:
文本
<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>
<span>Multiline message is:</span>
<p style="white-space: pre-line;">{{ message }}</p>//pre-line的含义是合并行序列保留换行符
<br>
<textarea v-model="message" placeholder="add multiple lines"></textarea>
<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">{{ checked }}</label>
<div id="example-4"><input type="radio" id="one" value="One" v-model="picked"><label for="one">One</label><br><input type="radio" id="two" value="Two" v-model="picked"><label for="two">Two</label><br><span>Picked: {{ picked }}</span>
</div>
<script>new Vue({el: '#example-4',data: {picked: ''}})
</script>
<div id="example-5"><select v-model="selected"><option disabled value="">请选择</option><option>A</option><option>B</option><option>C</option></select><span>Selected: {{ selected }}</span>
</div>
<script>new Vue({el: '...',data: {selected: ''}})
</script>
多选时 (绑定到一个数组):<div id="example-6"><select v-model="selected" multiple style="width: 50px;"><option>A</option><option>B</option><option>C</option></select><br><span>Selected: {{ selected }}</span>
</div>
<script>new Vue({el: '#example-6',data: {selected: []}})
</script>
<div id="app"><select v-model="selected" multiple><option :value="{num:1}">A</option><option :value="{num:2}">B</option><option :value="{num:3}">C</option><option :value="{num:4}">D</option></select><br><span>Selected: {{ selected }}</span>
</div>
<script>new Vue({el: '#app',data: {selected: []}})
</script>
<input v-model.lazy="msg"></input>
<div id="app"><component-a></component-a>
</div>
<script>//方法1Vueponent('component-a', { /* ... */ })//方法2var component = d({template: '<div><h3>全局组件</h3></div>>'})Vueponent('component-a', component)new Vue({ el: '#app' })
</script>
<script>var ComponentA = { /* ... */ }var ComponentB = { /* ... */ }var ComponentC = { /* ... */ }new Vue({el: '#app',components: {'component-a': ComponentA,'component-b': ComponentB}})
</script>
<div id="app"><my-com2></my-com2>
</div>
<script>Vueponent('myCom2',d({template: '<div>直接用component创建的组件</div>'}))new Vue({el: '#app'})
</script>
<div id="app"><my-com></my-com>
</div>
<template id="tmp"><h3>{{name}}</h3>
</template>
<script>Vueponent('my-com', {template: '#tmp',data: function (){return {name: 'john'}}})var app = new Vue({el: '#app'})
</script>
<div id="app"><h3>请输入要传的值<input type="text" v-model="title"></input></h3><child-node v-bind:parentTitle="title"></child-node>
</div>
<template id="tmp"><div><h4>属性值 {{content}} </h4></div>
</template>
<script>
var app = new Vue( {el: '#app',data(){return {title:'首页',}},components:{'childNode': {template: '#tmp',props: ['parentTitle'],data() {return{content: this.parentTitle}},watch: {parentTitle: {t = this.parentTitle}},}}
})
</script>
<div id="app"><h3>属性值 {{msg}} </h3><child-node @show="showMsg"></child-node>
</div>
<template id="tmp"><div><h4>请输入要传的值<input type="text" v-model="childMsg"></input><button @click="func">传递</button></h4></div>
</template>
<script>
var app = new Vue( {el: '#app',data(){return {title:'',msg: ''}},components:{'childNode': {template: '#tmp',data() {return{childMsg: ''}},methods: {func() {this.$emit('show', this.childMsg)}},}}
})
</script>
本文发布于:2024-01-31 14:27:34,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170668245429182.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |