vue入門基礎案例

炒菜背景

由於最近大家都喜歡vue這道菜,正好也在建站,所以就分享一下vue的原汁原味做法(基於官網的demo說明)。

之乎者也

Vue(讀作/vjuː/,類似於view)是一個用於構建用戶界面的漸進式框架。與其他單體框架不同,Vue從一開始就設計爲可增量採用。核心庫只關注於視圖層,並且很容易與其他庫或現有項目進行拾取和集成。另一方面,當與現代工具和支持庫結合使用時,Vue也完全能夠爲複雜的單頁應用程序提供支持。

菜名-八仙過海

如下是8個基礎demo,都放在html文件中,可以直接運行,你按照這個就可以完成vue入門。

<!DOCTYPE html>
<html>
	<head>
		<title>My first Vue app</title>
		//引入vue支持的包 "https://unpkg.com/vue"
		<br/>
		<script src="https://unpkg.com/vue"></script>
	</head>
	<body>
		//1、定義一個div,id=app,通過var app=new Vue({el: '#app',data:{}})完成雙向綁定。即js函數中data:數據的改變和頁面元素數據改變是關聯的。
		<div id="app">
			{{ message }}
		</div>
		<br>
		
		//2、v符號是vue特有標識,v-bin綁定title,可以靈活綁定該span的title渲染
		<div id="app-2">
			<span v-bind:title="message">
				Hover your mouse over me for a few seconds
				to see my dynamically bound title!
			</span>
		</div>
		<br />
		
		//3、v-if 通過js的seen:true/false 來實現元素的顯示/隱藏
		<div id="app-3">
		  <span v-if="seen">Now you see me</span>
		</div>
		<br />
		
		//4、 v-for 循環的使用。你可以在 js控制檯 輸入:app4.todos.push({text:'hh'});從而通過js代碼靈活加入新的元素。
		<div id="app-4">
		  <ol>
		    <li v-for="todo in todos">
		      {{ todo.text }}
		    </li>
		  </ol>
		</div>
		<br />
		
		//5、 v-on 綁定事件,v-on:click點擊事件,來顛倒p標籤內容。通過app5對象的methods來設置對應函數reverseMessage,
		
		<div id="app-5">
		  <p>{{ message }}</p>
		  <button v-on:click="reverseMessage">Reverse Message</button>
		</div>
		<br />
		
		//6、v-model指令,使表單輸入和應用程序狀態之間的雙向綁定變得輕而易舉:
		<div id="app-6">
		  <p>{{ message }}</p>
		  <input v-model="message">
		</div>
		<br/>
		
		//7、定義組件,如下組件todo-item例子簡單地實現了綁定。(注:vue組件和Custom Elements 自定義元素類似,定義符合Web Components Spec網絡組件規範標準。當然vue有更強的兼容性,如ie9+;
		<br/>
		//且Vue組件提供了普通自定義元素中不可用的重要功能,尤其是跨組件數據流、自定義事件通信和生成工具集成。)
		<div id='app-7'>
			<ol>
			  <!--組件的使用需要也需要完成app-7的綁定。 -->
			  <todo-item></todo-item>
			  
			</ol>
		</div>
		<br />
		
		//8、組件的進一步應用,如下實例,首先 v-for 循環獲取每行數據item,之後item綁定到組件屬性todo,item.id綁定到組件屬性key1。從而根據組件template渲染能數據
		<br />
		//(注意:啓動綁定的key 屬性不生效,可能由於key是關鍵字,key1是可以的。)
		<div id="app-8">
		  <ol>
		    <!--
		      Now we provide each todo-item with the todo object
		      it's representing, so that its content can be dynamic.
		      We also need to provide each component with a "key",
		      which will be explained later.
		    -->
		    <todo-item
		      v-for="item in groceryList"
		      v-bind:todo="item"
		      v-bind:key="item.id"
			  v-bind:key1="item.id"
		    ></todo-item>
		  </ol>
		</div>
		
		
		
		<script>
			var app = new Vue({
				el: '#app',
				data: {
					message: 'Hel2lo Vue1!'
				}
			})

			var app2 = new Vue({
				el: '#app-2',
				data: {
					message: 'You loaded this page on ' + new Date().toLocaleString()
				}
			})
			
			var app3 = new Vue({
			  el: '#app-3',
			  data: {
			    seen: true
			  }
			})
			
			var app4 = new Vue({
			  el: '#app-4',
			  data: {
			    todos: [
			      { text: 'Learn JavaScript' },
			      { text: 'Learn Vue' },
			      { text: 'Build something awesome' }
			    ]
			  }
			})
			
			var app5 = new Vue({
			  el: '#app-5',
			  data: {
			    message: 'Hello Vue.js!'
			  },
			  methods: {
			    reverseMessage: function () {
			      this.message = this.message.split('').reverse().join('')
			    }
			  }
			})
			
			var app6 = new Vue({
			  el: '#app-6',
			  data: {
			    message: 'Hello Vue!'
			  }
			})
			
			/* //app-7 組件 */
			Vue.component('todo-item', {
				template:'<li>this is a todo li</li>'
			})
			var aap7 = new Vue({
				el:'#app-7',
			})
			
			/* app-8 組件  */
			Vue.component('todo-item', {
			  // The todo-item component now accepts a
			  // "prop", which is like a custom attribute.
			  // This prop is called todo.
			  props: ['todo','key1'],
			  template: '<li >值:{{key1}}:{{todo.text }}</li>'
			})
			var app8 = new Vue({
			  el: '#app-8',
			  data: {
			    groceryList: [
			      { id: 0, text: 'Vegetables' },
			      { id: 1, text: 'Cheese' },
			      { id: 2, text: 'Whatever else humans are supposed to eat' }
			    ]
			  }
			})
			
			
		</script>

	</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章