1 DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8" />
5 <title>Vue 引出声明周期title>
6 <script type="text/javascript" src="../js/vue.js">script>
7 <script type="text/javascript" src="../js/day.min.js">script>
8 head>
9 <body>
10
20 <div id="root" v-cloak>
21 <h2 :style="{opacity}">BaiYeShuh2>
22 <button @click="opacity=1">透明度设置为1button>
23 <button @click="stop">点我停止变换button>
24 div>
25 body>
26
27 <script type="text/javascript" >
28 Vue.config.productionTip = false;// 阻止 vue 在启动时生成生产提示。
29
30 let vm = new Vue({
31 el: "#root",
32 data:{
33 opacity: 1,
34 },
35 methods:{
36 stop(e){
37 if (this.timerID) {
38 clearInterval(this.timerID);
39 this.timerID = 0;
40 }
41
42 }
43 },
44 mounted(){
45 this.timerID = setInterval(() => {
46 this.opacity -= 0.1;
47 if (this.opacity < 0) {
48 this.opacity = 1;
49 }
50 },50);
51 },
52 beforeDestroy(){
53 clearInterval(this.timerID);
54 }
55
56 });
57 script>
58 html>
1 DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8" />
5 <title>Vue 组件的基本使用title>
6 <script type="text/javascript" src="../js/vue.js">script>
7 <script type="text/javascript" src="../js/day.min.js">script>
8 head>
9 <body>
10
49 <div id="root" v-cloak>
50
51 <hello>hello>
52 <h1>{{msg}}h1>
53 <hr>
54 <school>school>
55 <hr>
56 <student>student>
57 <hr>
58 div>
59
60 <div id="root2" v-cloak>
61 <hello>hello>
62 div>
63 body>
64
65 <script type="text/javascript" >
66 Vue.config.productionTip = false;// 阻止 vue 在启动时生成生产提示。
67
68 // 创建组件
69 const hello = Vue.extend({
70 name:'hello1', // Vue开发环境中显示的名字
71 template:`<h1>你好啊!</h1>`
72 });
73
74 const school = Vue.extend({
75 template:`
76 <div>
77 <hello></hello>
78 <h3>学校名字:{{schoolName}}</h3>
/>
79 <h3>学校地址:{{schoolAddress}}</h3>
80 </div>
81 `,
82 data(){
83 return {
84 schoolName: 'BayYeShu',
85 schoolAddress: '深圳'
86 }
87 },
88 components:{ // 局部嵌套组件
89 hello
90 }
91 });
92
93 const student = Vue.extend({
94 template:`
95 <div>
96 <h3>学生名字:{{studentName}}</h3>
/>
97 <h3>学生地址:{{schoolAddress}}</h3>
98 </div>
99 `,
100 data(){
101 return {
102 studentName: 'Tom',
103 schoolAddress: '深圳-罗湖'
104 }
105 }
106 });
107
108 // 全局注册
109 Vue.component('hello',hello);
110
111 new Vue({
112 el: "#root",
113 data:{
114 msg: 'Hello world!'
115 },
116 // 局部注册
117 components:{
118 school: school,
119 student
120 }
121
122 });
123 new Vue({
124 el: "#root2",
125 });
126 script>
127 html>