var vm = new Vue ({
//选项
})
<div id="box" class="box"></div>
<html>
<head>
<meta charset="utf-8">
<title>vuedemo01title>
//引用Vue.js
<script src="vue.js" type="text/javascript" charset="utf-8">script>
head>
<body>
<div id="box">
<h3>网站名称:{
{name}}h3>
<h3>网站地址:{
{url}}h3>
div>
<script type="text/javascript">
var demo = new Vue ({
el : '#box',
data : {
name :'上海戏剧学院',//定义网站名称
url : 'www.shanghaixijuxueyuan.com'//定义网站地址
}
});
script>
body>
html>
<body>
<div id="box">
<h3>网站名称:{
{name}}h3>
<h3>网站地址:{
{url}}h3>
div>
<script type="text/javascript">
var data ={
name :'上海戏剧学院' , url:'www.shanghaixijuxueyuan.com'};
var demo = new Vue ({
el : '#box',
data : data
});
document.write(demo.name === data.name)//引用了相同的属性
demo.url = 'http://www.shanghaixijuxueyuan.com';//重新设置属性
script>
body>
data : {
name :''
count :''
price :''
flag :true
}
<script type="text/javascript">
var data ={
name :'上海戏剧学院' , url:'www.shanghaixijuxueyuan.com'};
var demo = new Vue ({
el : '#box',
data : data
});
document.write(demo.$data === data)//输出true
script>
<div id="box">
<h3>{
{showInfo()}}h3>
div>
<script type="text/javascript">
var demo = new Vue ({
el : '#box',
data : {
name :'上海戏剧学院',
url :'www.shanghaixijuxueyuan.com'
},
methods :{
showInfo : function(){
return this.name +':'+this.url;//连接字符串
}
}
});
script>
<div id="box">div>
<script type="text/javascript">
var demo = new Vue ({
el : '#box',
beforeCreate : function(){
console.log('beforeCreate');
},
created:function(){
console.log('created');
},
beforeDestroy:function(){
console.log('beforeDestroy');
},
destroyed:function(){
console.log('destroyed');
},
mounted:function(){
console.log('mounted');
this.$destroy();
}
});
script>
<div id="box">
<h3>{
{name}}h3>
div>
<script type="text/javascript">
var demo =new Vue({
el : '#box',
data :{
name :'你好,晓茗'//定义数据
}
})
script>
<div id="box">
<h3 v-once> Hello {
{name}}h3>
div>
<div id="box">
<p v-html="message">p>
div>
<script type="text/javascript">
var demo =new Vue({
el : '#box',
data :{
message :'Vue.js入门教程
'//定义数据
}
})
script>
<div id="box">
<span v-bind:class="value">面朝大海,春暖花开span>
div>
<script type="text/javascript">
var demo =new Vue({
el : '#box',
data :{
value :'title'//定义绑定的属性值
}
})
script>
body>
<style type="text/css">
.title{
color: #FF0000;
border: 1px solid #FF00FF;
display: inline-block;
padding: 5px;
}
style>
<div id="box">
<span v-bind:class="{
'title':value}">面朝大海,春暖花开span>
div>
<script type="text/javascript">
var demo =new Vue({
el : '#box',
data :{
value :true
}
})
script>
<a v-bind:href="url">上海戏剧学院a>
//简写格式如下:
<a :href="url" >上海戏剧学院a>
<div id="box">
{
{number+10}}<br>
{
{boo ? '真' : '假'}}<br>
{
{str.toLowerCase()}}
div>
<script type="text/javascript">
var demo =new Vue({
el : '#box',
data :{
number :10,
boo : true,
str :'你好,晓茗'
}
})
script>
{
{ message | myfilter }}
<div v-bind:id="rawId" | formatId">div>
Vue.filter(ID,funcation(){})
使用全局方法Vue.filter()定义的过滤器需要定义在创建的Vue实例之前
<div id="box">
<span>{
{date | nowdate}}span>
div>
<script type="text/javascript">
Vue.filter('nowdate',function(value){
var year=value.getFullYear();//获取当前年份
var month=value.getMonth()+1;//获取当前月份
var date=value.getDate();//获取当前日期
var day=value.getDay();//获取当前星期
var week="";
switch(day){
case 1:
week="星期一";
break;
case 2:
week="星期二";
break;
case 3:
week="星期三";
break;
case 4:
week="星期四";
break;
case 5:
week="星期五";
break;
case 6:
week="星期六";
break;
default:
week="星期日";
break;
}
return "今天是:"+year+"年"+month+"月"+date+"日"+week;
});
var demo = new Vue({
el : '#box',
data :{
date : new Date()
}
});
script>
<div id="box">
<ul>
<li><a href="#"><span>[特惠]span>{
{title1 | subStr}}a>li>
<li><a href="#"><span>[公告]span>{
{title2 | subStr}}a>li>
<li><a href="#"><span>[特惠]span>{
{title3 | subStr}}a>li>
ul>
div>
<script type="text/javascript">
var demo = new Vue({
el : '#box',
data :{
title1 :'商城爆品1分秒杀',
title2 :'家点狂欢千亿卷,买1送1!',
title3 :'全场清仓大处理,走过路过不要错过!'
},
filters:{
subStr : function(value){
if(value.length>10){
//如果字符串长度大于10
return value.substr(0,10)+"...";//返回字符串前10个字符,然后输出省略号
}else{
//如果字符串长度小于10
return value;//直接返回该字符串
}
}
}
});
script>
{
{ message | filterA | filterB }}
<div id="box">
<span>{
{str | lowercase |firstUppercase}}span>
div>
<script type="text/javascript">
var demo = new Vue({
el : '#box',
data :{
str : 'HTML+CSS+JavaScript'
},
filters:{
lowercase : function(value){
return value.toLowerCase();//转换为小写
},
firstUppercase : function(value){
return value.charAt(0).toUpperCase()+value.substr(1);//首字母大写
},
}
});
script>
{
{ message | filterA(arg1,arg2,......) }}
<img v-bind:src="imageSrc">
<button v-on:click="login">登录button>
<form v-on:submit.prevent="onSubmit">form>
备注:后期继续跟进Vue.js前端框架:条件判断与列表渲染,希望大家多多支持和关注。