Legendary

李洋的学习笔记


vue基础

<h2>简单粗暴,一个demo搞定vue基础 <sub>(标题逐渐UC化)</sub></h2> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt; &lt;title&gt;Document&lt;/title&gt; &lt;style&gt; /* 防闪 */ [v-clock] { display: none; } #app { border: 1px solid red; } .father { color: red; } .son { color: blue; } .sonBox { border: 1px solid blue; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="app" v-clock&gt; &lt;div&gt;{{str}}&lt;/div&gt; &lt;div&gt;绑定属性&lt;/div&gt; &lt;div&gt;1.method方法调用:{{meth(a)}}&lt;/div&gt; &lt;div&gt;2.computed方法名计算:{{com}}&lt;/div&gt; &lt;div&gt;3.filters参数串联过滤器方法名:{{c|fil|fil2}}&lt;/div&gt; &lt;global draggable="true"&gt;&lt;/global&gt; &lt;!-- 驼峰改蛇形 --&gt; &lt;my-tag class="sonBox" :Str="fatherStr" @changedata="changedata" :Arr="fatherArr" :Obj="fatherObj"&gt;&lt;/my-tag&gt; &lt;em&gt;父传子:子组件的props相当于父组件的data,声明变量来接收来自父组件的参数,只读=&gt; :son="father"&lt;/em&gt; &lt;div&gt;10.这是来自子组件的字符串:&lt;span class="son" @str="str(e)"&gt;{{}}&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;hr /&gt; 以下是app2 &lt;div id="app2"&gt; &lt;global&gt;&lt;/global&gt; &lt;!-- diy需要注册,否则无效 --&gt; &lt;!-- &lt;diy&gt;&lt;/diy&gt; --&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;script src="./node_modules/vue/dist/vue.min.js"&gt;&lt;/script&gt; &lt;script&gt; Vue.component('global', { template: `&lt;div&gt;4.全局template:不用注册,直接挂载到Vue上&lt;/div&gt;` }) let myTag = { template: `&lt;div&gt;&lt;div&gt;5.&lt;span&gt;局部template:&lt;/span&gt;&lt;a href='http://www.baidu.com'&gt;,需要注册,多个标签用一个div装起来&lt;/a&gt;&lt;/div&gt; &lt;div&gt;6.这是父组件传进来的字符串:&lt;span class="father"&gt;{{Str}}&lt;/span&gt;&lt;/div&gt; &lt;div&gt;7.这是父组件传进来的数组:[&lt;span class="father" v-for="item in Arr" class="father"&gt;{{item}},&lt;/span&gt;]&lt;/div&gt; &lt;div&gt;8.这是父组件传进来的对象:&lt;ul class="father"&gt;&lt;li v-for="item in Obj" :key="item.id"&gt;{{item.id}}--{{item.name}}&lt;/li&gt;&lt;/ul&gt; &lt;div&gt;9.这是子组件的按钮:&lt;button class="son" @click="changeBtn()"&gt;{{sonBtn}}&lt;/button&gt;&lt;/div&gt; &lt;/div&gt;`, props: ['Str', 'Arr', 'Obj'], // 子组件的data必须是一个返回装数据的对象的函数 data() { return { sonBtn: 1, changedata() { let parm = { str: '自定义事件改变父组件字符串', // arr:['木','火','土','金','水'], obj: [ { id: '子', name: '辰' }, { id: '丑', name: '巳' }, { id: '寅', name: '午' }, { id: '卯', name: '未' }, ] } this.$emit('changedata', parm) } } }, methods: { // 通过事件方法改变父组件数据 changeBtn() { this.changedata()// 共存时只自定义事件生效 this.$props.Str = '通过事件方法改变父组件数据' this.$props.Arr = [1, 2, 3, 4] this.$props.Obj = [ { id: 1, name: '甲' }, { id: 2, name: '乙' }, { id: 3, name: '丙' }, { id: 4, name: '丁' }, ] console.log(this.$props) this.sonBtn++ } } } new Vue({ el: "#app", data: { str: '双花括号语法', a: '1', b: '1', c: 1, fatherStr: '来自父组件的字符串', fatherArr: ['来', '自', '父', '亲'], fatherObj: [ { id: 1, name: '伯' }, { id: 2, name: '仲' }, { id: 3, name: '叔' }, { id: 4, name: '季' }, ] }, components: { myTag }, methods: { meth(a) { return a + 1 }, changedata(data) { console.log(data) this.fatherStr = data.str // this.fatherArr = data.arr this.fatherObj = data.obj } }, computed: { com() { return this.b } }, filters: { fil(c) { return c + 1 }, fil2(c) { return c + 'hhh' } }, // 生命周期 beforeCreate() { console.log("创建前:"); console.log(this.$el); console.log(this.$data); }, created() { console.log("创建完成:"); console.log(this.$el); console.log(this.$data); }, beforeMount() { console.log("挂载前:"); console.log(this.$el); console.log(this.$data); }, mounted() { console.log("挂载完成:"); console.log(this.$el); console.log(this.$data); }, beforeUpdate() { console.log('=即将更新渲染='); //let name = this.$refs.app.innerHTML; console.log('name:' + name); }, updated() { console.log('==更新成功=='); //let name = this.$refs.app.innerHTML; console.log('name:' + name); }, beforeDestory() { console.log("销毁前:"); }, destoryed() { console.log("销毁完成:"); } }) new Vue({ el: '#app2', // components:{diy:myTag},//diy注册,否则挂载无效 }) &lt;/script&gt;</code></pre>

页面列表

ITEM_HTML