vue.js基礎demo

Vue.js

定義:

Vue (讀音 /vjuː/,類似於 view) 是一套用於構建用戶界面的漸進式JavaScript框架。與其它大型框架不同的是,Vue 被設計爲可以自底向上逐層應用。Vue 的核心庫只關注視圖層,方便與第三方庫或既有項目整合

Vue.js 目標

通過儘可能簡單的 API 實現響應的數據綁定和組合的視圖組

目錄結構

目錄/文件

說明

build

項目構建(webpack)相關代碼

config

配置目錄,包括端口號等。我們初學可以使用默認的。

node_modules

npm 加載的項目依賴模塊

src

包含了幾個目錄及文件:

  •  

assets: 放置一些圖片,如logo等。

  •  
  •  

components: 目錄裏面放了一個組件文件,可以不用。

  •  
  •  

App.vue: 項目入口文件,我們也可以直接將組件寫這裏,而不使用 components 目錄。

  •  
  •  

main.js: 項目的核心文件。

  •  

static

靜態資源目錄,如圖片、字體等。

test

初始測試目錄,可刪除

.xxxx文件

這些是一些配置文件,包括語法配置,git配置等。

index.html

首頁入口文件,你可以添加一些 meta 信息或統計代碼啥的。

package.json

項目配置文件。

README.md

項目的說明文檔,markdown 格式

參考資料: [4] 

 

 

 

 

Vue.js教程

Demo1:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Vue 測試實例 - 菜鳥教程(runoob.com)</title>

<script src="https://unpkg.com/vue/dist/vue.js"></script>

</head>

<body>

<div id="app">

  <p>{{ message }}</p>

</div>

 

<script>

new Vue({

  el: '#app',

  data: {

    message: 'Hello Vue.js!'

  }

})

</script>

</body>

</html>

Vue.js安裝

我們可以在 Vue.js 的官網上直接下載 vue.min.js 並用 <script> 標籤引入

使用cdn方法

Staticfile CDN(國內) :https://cdn.staticfile.org/vue/2.2.2/vue.min.js

unpkghttps://unpkg.com/vue/dist/vue.js

cdnjs : https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.js

使用方法如例:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Vue 測試實例 - 菜鳥教程(runoob.com)</title>

<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

//script引入

</head>

<body>

<div id="app">

  <p>{{ message }}</p>

</div>

 

<script>

new Vue({

  el: '#app',

  data: {

    message: 'Hello Vue.js!'

  }

})

</script>

</body>

</html>

 

 

NPM方法:

#查看版本

$ npm -v

#升級

cnpm install npm -g

#升級或安裝

cnpm install cnpm -g

$cnpm install vue(用vue.js構建大型應用時使用)

命令行工具

#全局安裝 vue-cli

$ cnpm install --global vue-cli

#創建一個基於webpack模板的新項目

$vue init webpack my-project

#配置,默認回車

This will install Vue 2.x version of the template.

For Vue 1.x use:vue init webpack#1.0 my-project

? Project name my-project

? Project description A Vue.js project

? Author runoob <[email protected]>

? Vue build standalone? Use ESLint to lint your code? Yes

? Pick an ESLint preset Standard? Setup unit tests with Karma + Mocha? Yes

? Setup e2e tests with Nightwatch? Yes

   vue-cli · Generated "my-project".

To get started:

  cd my-project

     npm install

     npm run dev

   

進入項目,安裝並運行:

$ cd my-project

$ cnpm install

$ cnpm run dev

 DONE  Compiled successfully in 4388ms

> Listening at http://localhost:8080

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

  {{ message }} {{name}}

</div>

 

<script type="text/javascript">

var app = new Vue({

el: '#app',

data: {

message: 'Hello Vue!',

name : "Vue"

}

});

</script>

 

</body>

</html>

 

 

### 安裝 `npm`

`npm` 全稱爲 `Node Package Manager`,是一個基於`Node.js`的包管理器,也是整個`Node.js`社區最流行、支持的第三方模塊最多的包管理器。

```

npm -v

```

 

### 由於網絡原因 安裝 `cnpm`

```

npm install -g cnpm --registry=https://registry.npm.taobao.org

```

 

### 安裝 `vue-cli`

```

cnpm install -g @vue/cli

```

 

### 安裝 `webpack`

`webpack` 是  `JavaScript` 打包器(module bundler)

```

cnpm install -g webpack

```

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

{{msg}}

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

msg : "hi vue",

},

//在實例初始化之後,數據觀測 (data observer) 和 event/watcher 事件配置之前被調用。

beforeCreate:function(){

console.log('beforeCreate');

},

/* 在實例創建完成後被立即調用。

在這一步,實例已完成以下的配置:數據觀測 (data observer),屬性和方法的運算,watch/event 事件回調。

然而,掛載階段還沒開始,$el 屬性目前不可見。 */

created :function(){

console.log('created');

},

//在掛載開始之前被調用:相關的渲染函數首次被調用

beforeMount : function(){

console.log('beforeMount');

 

},

//el 被新創建的 vm.$el 替換, 掛在成功

mounted : function(){

console.log('mounted');

 

},

//數據更新時調用

beforeUpdate : function(){

console.log('beforeUpdate');

 

},

//組件 DOM 已經更新, 組件更新完畢

updated : function(){

console.log('updated');

 

}

});

setTimeout(function(){

vm.msg = "change ......";

}, 3000);

</script>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

{{msg}}

<p>Using mustaches: {{ rawHtml }}</p>

<p v-html="rawHtml"></p>

<div v-bind:class="color">test...</div>

<p>{{ number + 1 }}</p>

<p>{{ 1 == 1 ? 'YES' : 'NO' }}</p>

<p>{{ message.split('').reverse().join('') }}</p>

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

msg : "hi vue",

rawHtml : '<span style="color:red">this is should be red</span>',

color:'blue',

number : 10,

ok : 1,

message : "asvue"

}

});

vm.msg = "hi....";

</script>

<style type="text/css">

.red{color:red;}

.blue{color:blue; font-size:100px;}

</style>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<p v-if="seen">現在你看到我了</p>

<a v-bind:href="url">...</a>

<div @click="click1">

<div @click.stop="click2">

click me

</div>

</div>

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

seen : true,

url : "https://cn.vuejs.org/v2/guide/syntax.html#%E6%8C%87%E4%BB%A4"

},

methods:{

click1 : function () {

 

console.log('click1......');

},

click2 : function () {

// vm.url=null;

console.log('click2......');

}

}

});

</script>

<style type="text/css">

 

</style>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<div

class="test"

v-bind:class="[ isActive ? 'active' : '', isGreen ? 'green' : '']"

style="width:200px; height:200px; text-align:center; line-height:200px;">

hi vue

</div>

 

<div

:style="{color:color, fontSize:size, background: isRed ? '#FF0000' : ''}">

hi vue

</div>

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

isActive : true,

isGreen : true,

color : "#FFFFFF",

size : '50px',

isRed : true

}

});

</script>

<style>

.test{font-size:30px;}

.green{color:#00FF00;}

.active{background:#FF0000;}

</style>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<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>

<h1 v-show="ok">Hello!</h1>

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

type : "A",

ok : false

}

});

</script>

<style type="text/css">

 

</style>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<ul>

<li v-for="item,index in items" :key="index">

{{index+1}}{{ item.message }}

</li>

</ul>

<ul>

<li v-for="value, key in object">

{{key}} : {{ value }}

</li>

</ul>

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

items : [

{ message: 'Foo' },

{ message: 'Bar' },

{ message: 'Bar' }

],

object: {

title: 'How to do lists in Vue',

author: 'Jane Doe',

publishedAt: '2016-04-10'

}

}

});

</script>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<div id="example-1">

<button v-on:click="counter += 1"> 數值 :  {{ counter }} </button><br />

<button v-on:dblclick="greet('abc', $event)">Greet</button>

</div>

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

counter: 0,

// name : "vue"

},

methods:{

greet : function (str, e) {

alert(str);

console.log(e);

}

}

});

</script>

<style type="text/css">

 

</style>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<div id="example-1">

<input v-model="message" placeholder="edit me">

<p>Message is: {{ message }}</p>

<textarea v-model="message2" placeholder="add multiple lines"></textarea>

<p style="white-space: pre-line;">{{ message2 }}</p>

<br />

 

<div style="margin-top:20px;">

<input type="checkbox" id="jack" value="Jack" v-model="checkedNames">

<label for="jack">Jack</label>

<input type="checkbox" id="john" value="John" v-model="checkedNames">

<label for="john">John</label>

<input type="checkbox" id="mike" value="Mike" v-model="checkedNames">

<label for="mike">Mike</label>

<br>

<span>Checked names: {{ checkedNames }}</span>

</div>

 

<div style="margin-top:20px;">

<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>

<button type="button" @click="submit">提交</button>

</div>

 

</div>

<script type="text/javascript">

var vm = new Vue({

el : "#app",

data : {

message : "test",

message2 :"hi",

checkedNames : ['Jack', 'John'],

picked : "Two"

},

methods: {

submit : function () {

console.log(this.message2);

 

}

}

});

</script>

<style type="text/css">

 

</style>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<button-counter title="title1 : " @clicknow="clicknow">

<h2>hi...h2</h2>

</button-counter>

<button-counter title="title2 : "></button-counter>

<button-counter title="title3 : "></button-counter>

</div>

<script type="text/javascript">

Vue.component('button-counter', {

props: ['title'],

data: function () {

return {

  count: 0

}

},

template: '<div><h1>hi...</h1><button v-on:click="clickfun">{{title}} You clicked me {{ count }} times.</button><slot></slot></div>',

methods:{

clickfun : function () {

this.count ++;

this.$emit('clicknow', this.count);

}

}

})

var vm = new Vue({

el : "#app",

data : {

 

},

methods:{

clicknow : function (e) {

console.log(e);

}

}

});

</script>

<style type="text/css">

 

</style>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script src="vue.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>

<div id="app">

<button-counter></button-counter>

<test></test>

<button-counter></button-counter>

</div>

<script type="text/javascript">

Vue.component('button-counter', {

props: ['title'],

data: function () {

return {}

},

template: '<div><h1>hi...</h1></div>',

methods:{

 

}

})

var vm = new Vue({

el : "#app",

data : {

 

},

methods:{

clicknow : function (e) {

console.log(e);

}

},

components:{

test : {

template:"<h2>h2...</h2>"

}

}

});

</script>

<style type="text/css">

 

</style>

</body>

</html>

 

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