重点:引入vue.js
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="vue.js" type="text/javascript" charset="utf-8">script>
head>
<body>
body>
html>
重点:{{ message }}
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>
重点:vm实例、$ data、$ watch
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">
{{a}}
div>
<script type="text/javascript">
var data = { a : 1 };
var vm = new Vue({
el : "#app",
data : data
});
vm.$watch('a', function(newVal, oldVal){
console.log(newVal, oldVal);
})
vm.$data.a = "test...."
script>
body>
html>
重点:created、mounted 、updated
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 : 'this is should be red',
color:'blue',
number : 10,
ok : 1,
message : "vue"
}
});
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 : false,
url : "https://cn.vuejs.org/v2/guide/syntax.html#%E6%8C%87%E4%BB%A4"
},
methods:{
click1 : function () {
console.log('click1......');
},
click2 : function () {
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 : "B",
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}}{{ 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' }
],
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)">Greetbutton>
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">Jacklabel>
<input type="checkbox" id="john" value="John" v-model="checkedNames">
<label for="john">Johnlabel>
<input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
<label for="mike">Mikelabel>
<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">Onelabel>
<br>
<input type="radio" id="two" value="Two" v-model="picked">
<label for="two">Twolabel>
<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.message);
}
}
});
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...h2h2>
button-counter>
<button-counter title="title2 : ">button-counter>
div>
<script type="text/javascript">
Vue.component('button-counter', {
props: ['title'],
data: function () {
return {
count: 0
}
},
template: 'hi...
',
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>
div>
<script type="text/javascript">
Vue.component('button-counter', {
props: ['title'],
data: function () {
return {}
},
template: 'hi...
',
methods:{
}
})
var vm = new Vue({
el : "#app",
data : {
},
methods:{
clicknow : function (e) {
console.log(e);
}
},
components:{
test : {
template:"h2...
"
}
}
});
script>
<style type="text/css">
style>
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