2019-7-4 seo達人
如果您想訂閱本博客內(nèi)容,每天自動發(fā)到您的郵箱中, 請點這里
    
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Vue 測試實例</title>
 <script src="../static/vue.min.js"></script>
</head>
<body>
 <div id="vue_data">
 <h1>title : {{title}}</h1>
 <h1>url : {{url}}</h1>
 <h1>{{info()}}</h1>
 </div>
 <script type="text/javascript">
 var vm = new Vue({
 el: '#vue_data',
 data: {
 title: "Vue.js",
 url: "https://cn.vuejs.org"
 },
 methods: {
 info: function() {
 return  this.title + " - 堅持學習!";
 }
 }
 })
 </script>
</body>
</html>
1、每個Vue應用都需要實例化Vue來實現(xiàn)
var vm = new Vue({
    //*******
})
2、el參數(shù)
在上面實例中的id為vue_data,在div元素中:
<div id="vue_data"></div>
意味著所有的改動均在這個id為vau_data的div中,外部不受影響。
3、定義數(shù)據(jù)對象
data用于定義屬性,在上述實例中有2個屬性,分別為:title、url。
methods用于定義函數(shù),可以通過return來返回函數(shù)值。
{{ }}用于輸出對象屬性和函數(shù)返回值。
當一個Vue實例創(chuàng)建時,Vue的響應系統(tǒng)加入了data對象中能找到的所有屬性。當這些屬性的值發(fā)生改變時,html視圖也會產(chǎn)生相應的變化。
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Vue 測試實例</title>
 <script src="../static/vue.min.js"></script>
</head>
<body>
 <div id="vue_data">
 <h1>title : {{title}}</h1>
 <h1>url : {{url}}</h1>
 </div>
 <script type="text/javascript">
 //數(shù)據(jù)對象
 var data = {title: "Vue.js",url: "https://cn.vuejs.org"}
 var vm = new Vue({
 el: '#vue_data',
 data: data
 })
 
 //設置屬性會影響到原始數(shù)據(jù)
 vm.title = "spring";
 document.write(data.title + "<br>");
 
 //同樣
 data.url = "https://spring.io";
 document.write(vm.url);
 </script>
</body>
</html>
Vue還提供了實例屬性與方法,以前綴$與用戶定義的屬性區(qū)分開來。
document.write(vm.$data === data) // true
藍藍設計( m.wnxcall.com )是一家專注而深入的界面設計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網(wǎng)站建設 、平面設計服務。