Vue.js 教程

摘要:
'}})3.输出html文本-----v-bind:idv-bind:classv-bind:titlevue多属性绑定

1.vue.js主题结构如下:

<!--Create by syd on 2018/9/4 17:07.-->
<html len="en">
<header>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="/src/css/main.css">
    <title>Vue之入门HelloWorld</title>
    <!--引入Vue库-->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style type="text/stylesheet">
        [v-cloak] {
            display: none;
        }
    </style>
</header>
<body> 
    <div id="app"> 
        <p>{{ message }}</p> 
    </div> <script> 
    new Vue({ 
         el: '#app', 
         data: { message: 'Hello World!' }
    })
</script>
</body>
</html>

2.文本数据绑定

<div id="app">
    <p>{{ message }}</p>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    message: 'Hello World!'
  }
})
</script>

3.输出html文本----- v-bind:id  v-bind:class v-bind:title

<div id="app">
    <h2 v-bind:id="vid" :class="vclass" :title="vtitle">vue 多属性绑定</h2>
</div>
    
<script>
new Vue({
    el: "#app",
    data: {
        vid:"myid",
        vclass: "myclass",
        vtitle:"vue 多属性绑定"
    }
})

4.vue.js计算属性

<!-- vue.js计算属性 -->
<div id="app">
a={{ a }}, b={{ b }}
</div>
<script>
new Vue({
    el: '#app',
    data: {
        a: 1
    },
// 计算属性

computed: { b() { return this.a + 1 } } }) </script>

 setter && getter方法

<div id="app">{{message}}</div>
<script>
var vm = new Vue({
    el: '#app',
    data: {
        title: 'my first lesson'
    },
    computed: {
        message: {
            // getter
            get: function () {
                return this.title
            },
            // setter
            set: function (newValue) {
                this.title = newValue
            }
        }
    }
})
vm.message = 'my second lesson';
</script>

Ps:完整基础知识

<!--Create by syd on 2018/9/4 17:07.-->
<html len="en">
<header>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="/src/css/main.css">
    <title>Vue之入门HelloWorld</title>
    <!--引入Vue库-->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style type="text/stylesheet">
        [v-cloak] {
            display: none;
        }
    </style>
</header>
<body>
   <!--创建一个div-->
   <div id="app">
       <!-- Vue数据绑定 v-text取值 -->
       <!-- vue.js语法模板 -->
       <p v-text="message"></p>
       <!-- 输出HTML文件--html文件绑定属性 -->
       <h2 :class="htmlMessage">v-bind:进行属性绑定</h2>
       <!-- vue.js多属性绑定 -->
       <h2 :id="vid" :class="vClass" :title="vTitle">vue多属性绑定</h2>

       <!-- vue.js计算属性 -->
       <div>
           a = {{a}},b = {{b}}
       </div>

       <div v-if="isLogin">你好</div>
       <div v-else>请登录后再操作</div>
       <div v-show="isLogin">你好</div>
       <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>
       <div class="ui-li">
           <ul>
               <li v-for="item in items"><h5 v-text="item"></h5></li>
           </ul>
       </div>
       <div>
           <ul>
               <li v-for="(value,key,index) in object">{{index}}.{{key}}.{{value}}</li>
           </ul>
       </div>
       <div>本场得分:{{count}}</div>
       <button @click="add">加分</button>
       <!-- v-model 初始化-->
       <div>
           <input type="text" v-model="message"><br>
           <textarea cols="30" rows="10" v-model="message"></textarea>
           <input type="checkbox" :id="first" value="1" v-model="status">
           <label for="first">有效</label>
           <input type="checkbox" :id="second" value="2" v-model="status">
           <label for="second">无效</label>
           <div>状态:{{status}}</div>
       </div>
       <div>
           <input type="radio" id="one" value="男" v-model="sex">
           <label for="one"></label>
           <input type="radio" id="two" value="女" v-model="sex">
           <label for="two"></label>
           <div>性别:{{sex}}</div>
       </div>
       <div>
           <select v-model="selected">
               <option disabled value="">请选择</option>
               <option v-for="item in choice">{{item}}</option>
           </select>
           <div>Selected: {{selected}}</div>
       </div>
       <!-- 动态赋值 -->
       <div>
           <img :src="imgSrc" width="200px">
       </div>
       <!-- 输出原始值 -->
       <div v-pre>{{message}}</div>
       <div v-cloak>{{message}}</div>
       <div v-once>第一次绑定的值:{{message}}</div>
   </div>

   <!-- JS -->
   <script type="text/javascript">
       var app = new Vue({ // 创建Vue对象
           el: '#app', // #app是id选择器,把当前Vue挂载到div上
           data (){  // Vue中绑定的数据
               return {
                   message: 'hello Vue!',
                   htmlMessage: "content",
                   vid:"myId",
                   vClass:"myClass",
                   vTitle:"vue 多属性",
                   isLogin: true,
                   type: 'A',
                   items: [20,23,18,65],
                   object: {firstName:'john',lastName:'Doe'},
                   count: 1,
                   status: [],
                   sex: '',
                   selected: '',
                   choice: ['A','B','C','D','E'],
                   imgSrc:'http://liangxinghua.com/uploads/image/20180709/1531106987.png',
                   price: '3',
                   a: 1,

               }
           },
           beforeCreate: function () {
               console.group('beforeCreate 创建前状态===============》');
               console.log("%c%s", "color:red" , "el      : " + this.$el); //undefined
               console.log("%c%s", "color:red","data    : " + this.$data); //undefined
               console.log("%c%s", "color:red","message: " + this.message)
           },
           created: function () {
               console.group('created 创建完毕状态===============》');
               console.log("%c%s", "color:red","el      : " + this.$el); //undefined
               console.log("%c%s", "color:red","data    : " + this.$data); //已被初始化
               console.log("%c%s", "color:red","message: " + this.message); //已被初始化
           },
           beforeMount: function () {
               console.group('beforeMount 挂载前状态===============》');
               console.log("%c%s", "color:red","el      : " + (this.$el)); //已被初始化
               console.log(this.$el);
               console.log("%c%s", "color:red","data    : " + this.$data); //已被初始化
               console.log("%c%s", "color:red","message: " + this.message); //已被初始化
           },
           mounted: function () {
               console.group('mounted 挂载结束状态===============》');
               console.log("%c%s", "color:red","el      : " + this.$el); //已被初始化
               console.log(this.$el);
               console.log("%c%s", "color:red","data    : " + this.$data); //已被初始化
               console.log("%c%s", "color:red","message: " + this.message); //已被初始化
           },
           beforeUpdate: function () {
               console.group('beforeUpdate 更新前状态===============》');
               console.log("%c%s", "color:red","el      : " + this.$el);
               console.log(this.$el);
               console.log("%c%s", "color:red","data    : " + this.$data);
               console.log("%c%s", "color:red","message: " + this.message);
           },
           updated: function () {
               console.group('updated 更新完成状态===============》');
               console.log("%c%s", "color:red","el      : " + this.$el);
               console.log(this.$el);
               console.log("%c%s", "color:red","data    : " + this.$data);
               console.log("%c%s", "color:red","message: " + this.message);
           },
           beforeDestroy: function () {
               console.group('beforeDestroy 销毁前状态===============》');
               console.log("%c%s", "color:red","el      : " + this.$el);
               console.log(this.$el);
               console.log("%c%s", "color:red","data    : " + this.$data);
               console.log("%c%s", "color:red","message: " + this.message);
           },
           destroyed: function () {
               console.group('destroyed 销毁完成状态===============》');
               console.log("%c%s", "color:red","el      : " + this.$el);
               console.log(this.$el);
               console.log("%c%s", "color:red","data    : " + this.$data);
               console.log("%c%s", "color:red","message: " + this.message)
           },
           // 计算属性
           computed: {
               newPrice(){
                   return '' + this.price + ''
               },
               b(){
                   return this.a + 1;
               }
           },
           // 事件绑定方法
           methods: {
               add(){
                   this.count++;
               },
               add(num){
                   this.count++;
               }
           },
           // 观察者
           watch:{
               question(val,oldVal){
                   console.log('new:%s,old:%s',val,oldVal);
               }
           },
           // 过滤器
           filters:{
               filterA(value){
                   return value.toUpperCase();
               }
           }

       })
   </script>

</body>
</html>

免责声明:文章转载自《Vue.js 教程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇GDB高级使用方法Hello China操作系统的安装和使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

深入理解php底层:php生命周期

1、PHP的运行模式: PHP两种运行模式是WEB模式、CLI模式。无论哪种模式,PHP工作原理都是一样的,作为一种SAPI运行。 1、当我们在终端敲入php这个命令的时候,它使用的是CLI。 它就像一个web服务器一样来支持php完成这个请求,请求完成后再重新把控制权交给终端。 2、当使用Apache或者别web服务器作为宿主时,当一个请求到来时,P...

宜信数据采集平台DBus-allinone部署实战案例

          宜信数据采集平台DBus-All In One部署实战案例                                      作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任。   官方文档可参考:https://github.com/BriData/DBus/blob/master/docs/quick-star...

单项目实现vendor分离编译,增加编译效率(vue-cli)

1、在build文件夹下添加文件:webpack.dll.config.js const path = require('path') const webpack = require('webpack') const package = require('../package.json') const AssetsPlugin = require('ass...

Tushare Pro接口介绍

  Tushare库在数据获取方面一直受到金融分析人员的青睐,极大地减轻了他们在金融数据采集、清洗加工、存储过程的工作量,更加专注于策略和模型的研究与实现上。由于Tushare旧版运行了有3年之久,因此目前网上很多涉及财经和股票交易数据获取的文章使用的是旧版Tushare。Tushare社区现在主要维护新版本tushare pro,它的数据更稳定质量更高,...

原生js和vue之间的数据通讯--EventEmitter

有个小项目在原来原生的框架编写,但是不想写原生,就引入了vue 然后有个需求要和原生的js进行交互通讯,于是就可以用node.js EventEmitter 具体做法: 先引入文件<script src="http://t.zoukankan.com/js/eventEmitter.js"></script>, 初始化, 然后在vu...

vuejs如何调试代码

基于webpack的配置调试# 使用Vue-cli命令行工具初始化基于wabpack模板的项目的命令语法: Copy npm install -g @vue/cli # 全局安装vue-cli,版本vue3.x vue init webpack [my-project] [app-name] # 使用vue-cli初始化...