• vue 基础语法(二)


    12-(掌握)插值操作-mustache语法

    1. <div id="app">
    2. <h3>{{msg}}h3>
    3. <h3>{{msg}},李银河h3>
    4. <h3>{{firstName+"·"+lastName}}h3>
    5. <h3>{{firstName}} {{lastName}}h3>
    6. <h4>{{counter*2}}h4>
    7. div>
    8. <script src = "./js/vue.js">script>
    9. <script>
    10. /**
    11. * 插值操作
    12. * 1、{{}} mustache 双大括号语法 数据是响应的
    13. */
    14. const app = new Vue({
    15. el:"#app",
    16. data:{
    17. msg:"你好呀",
    18. firstName:"乌拉那拉",
    19. lastName:"宜修",
    20. counter:100
    21. }
    22. })
    23. script>

     13-(掌握)插值操作-其他指令使用

    1. <div id="app" v-cloak>
    2. <h3>{{msg}}h3>
    3. <h3 v-once>{{msg}}h3>
    4. <div v-html = "url">div>
    5. <h2 v-text = "msg">h2>
    6. <h2 v-pre >{{msg}}h2>
    7. div>
    8. <script src = "./js/vue.js">script>
    9. <script>
    10. /**
    11. * 插值操作 其他指令
    12. * 1、v-once
    13. * 2、v-html
    14. * 3、v-text 一般不适用,因为它不够灵活
    15. * 4、v-pre 原封不动的展示内容 用的也很少
    16. * 5、v-cloak cloak:斗篷 没啥意义,仅做了解
    17. *
    18. * 在vue解析之前,div有v-cloak 属性
    19. * 在vue解析之后,div中没有v-cloak 属性
    20. */
    21. const app = new Vue({
    22. el:"#app",
    23. data:{
    24. msg:"你好呀",
    25. url:"百度一下",
    26. }
    27. })
    28. script>

    14-(掌握)v-bind的基本使用

    1. body>
    2. <div id="app" v-cloak>
    3. <h3>{{msg}}h3>
    4. <img v-bind:src="imgurl" alt="">
    5. <a v-bind:href="aHref">百度一下a>
    6. <a :href="aHref">百度一下a>
    7. div>
    8. <script src = "./js/vue.js">script>
    9. <script>
    10. /**
    11. * v-bind 动态绑定属性
    12. * 给哪个属性绑定值,就在那个属性前面加 v-bind:
    13. * 语法糖: v-bind:href => :href
    14. */
    15. const app = new Vue({
    16. el:"#app",
    17. data:{
    18. msg:"你好呀",
    19. imgurl:"https://v2.cn.vuejs.org/images/lifecycle.png",
    20. aHref:"http://www.baidu.com"
    21. }
    22. })
    23. script>
    24. body>

    15-(掌握)v-bind动态绑定class(对象语法)【注意案例里面的简单写法】

    1. <div id="app" v-cloak>
    2. <div :class = "{active:isActive,line:isLine}">测试div>
    3. <div class = "title" :class = "{active:isActive,line:isLine}">测试div>
    4. <div class = "title" :class = "getClass()">测试div>
    5. <button :class = "{changeColor:isChangeColor}" @click = "clickFn">按钮button>
    6. div>
    7. <script src = "./js/vue.js">script>
    8. <script>
    9. /**
    10. * v-bind 动态绑定class (对象语法)
    11. * 1、 v-bind:class = "{'类名1':boolean,'类名2':boolean}"
    12. * 2、如果类名太长,可以写在computed里面或者methods里面
    13. */
    14. const app = new Vue({
    15. el:"#app",
    16. data:{
    17. msg:"hello world",
    18. active:"active",
    19. isActive:true,
    20. isLine:true,
    21. isChangeColor:false,
    22. counter:0
    23. },
    24. methods:{
    25. getClass(){
    26. return {active:this.isActive,line:this.isLine}
    27. },
    28. clickFn(){
    29. // 注意这里的一个简单写法
    30. // this.counter++;
    31. // if(this.counter%2==0){
    32. // this.isChangeColor = false
    33. // }else{
    34. // this.isChangeColor = true
    35. // }
    36. this.isChangeColor = !this.isChangeColor
    37. }
    38. }
    39. })
    40. script>

    16-(了解)v-bind动态绑定class(数组语法)  【仅做了解,用的很少】

    1. <div id="app" v-cloak>
    2. <div class="title" :class = "['active','line']">{{msg}}div>
    3. <div class="title" :class = "[active,line]">{{msg}}div>
    4. <div class="title" :class = "getClasses()">{{msg}}div>
    5. div>
    6. <script src = "./js/vue.js">script>
    7. <script>
    8. /**
    9. * v-bind 动态绑定class (数组语法)
    10. * 1、多个类名可以写在computed或者methods里面
    11. */
    12. const app = new Vue({
    13. el:"#app",
    14. data:{
    15. msg:"hello world",
    16. active:"aaa",
    17. line:"bbb"
    18. },
    19. methods:{
    20. getClasses(){
    21. return [this.active,this.line]
    22. }
    23. }
    24. })
    25. script>

     17-(完成)v-bind和v-for结合的作业布置

    1. <div id="app" v-cloak>
    2. <ul>
    3. <li v-for = "(item,index) of movies" :class = "{active:currentIndex == index}" @click = "clickFn(index)">{{item}}li>
    4. ul>
    5. div>
    6. <script src = "./js/vue.js">script>
    7. <script>
    8. /**
    9. * v-bind 和 v-for 小作业:
    10. * 实现列表,默认第一个高亮,然后点击哪个哪个高亮
    11. */
    12. const app = new Vue({
    13. el:"#app",
    14. data:{
    15. msg:"hello world",
    16. movies:["甄嬛传","芈月传","如懿传"],
    17. currentIndex:0
    18. },
    19. methods:{
    20. clickFn(index){
    21. this.currentIndex = index
    22. }
    23. }
    24. })
    25. script>

    18-(掌握)v-bind动态绑定style(对象语法)

    1. <div id="app" v-cloak>
    2. <div :style = "{fontSize:fontSize}">{{msg}}div>
    3. <div :style = "{'font-size':fontSize}">{{msg}}div>
    4. <div :style = "{'font-size':finalSize+'px'}">{{msg}}div>
    5. <div :style = "{'font-size':fontSize,color:finalColor}">{{msg}}div>
    6. <div :style = "getStyles()">{{msg}}div>
    7. div>
    8. <script src = "./js/vue.js">script>
    9. <script>
    10. /**
    11. * v-bind 动态绑定style(对象语法)
    12. * 在组件化开发中用的多一些
    13. * 对象语法用的比较多
    14. * 属性太多可以抽离到methods或者computed里面
    15. * */
    16. const app = new Vue({
    17. el:"#app",
    18. data:{
    19. msg:"hello world",
    20. fontSize:"28px",
    21. finalSize:52,
    22. finalColor:"red"
    23. },
    24. methods:{
    25. getStyles(){
    26. return {'font-size':this.fontSize,color:this.finalColor}
    27. }
    28. }
    29. })
    30. script>

    19-(了解)v-bind动态绑定style(数组语法)

     

     20-(掌握)计算属性的基本使用 【计算属性是当做属性使用的,使用的时候不需要加()

    1. <div id="app">
    2. <div>{{firstname}} {{lastname}}div>
    3. <div>{{firstname+" "+lastname}}div>
    4. <div>{{getFullName()}}div>
    5. <div>{{fullName}}div>
    6. div>
    7. <script src = "./js/vue.js">script>
    8. <script>
    9. /**
    10. * 计算属性:当我们要对某种数据进行变化的时候再来进行显示的话,可以使用computed
    11. * 计算属性是当做属性使用的,使用的时候不需要加()
    12. * */
    13. const app = new Vue({
    14. el:"#app",
    15. data:{
    16. firstname:"hello",
    17. lastname:"world"
    18. },
    19. computed:{
    20. // 计算属性一般不加动词
    21. fullName(){
    22. return this.firstname+" "+this.lastname;
    23. }
    24. },
    25. methods:{
    26. getFullName(){
    27. return this.firstname+" "+this.lastname;
    28. }
    29. }
    30. })
    31. script>

    21-(掌握)计算属性的复杂操作

    1. <div id="app">
    2. <div>{{firstname}} {{lastname}}div>
    3. <div>{{firstname+" "+lastname}}div>
    4. 总价格:{{totalPirce}}
    5. div>
    6. <script src = "./js/vue.js">script>
    7. <script>
    8. const app = new Vue({
    9. el:"#app",
    10. data:{
    11. firstname:"hello",
    12. lastname:"world",
    13. books:[
    14. {
    15. id:"001",
    16. name:"unix编程艺术",
    17. price:1
    18. },
    19. {
    20. id:"002",
    21. name:"代码大全",
    22. price:2
    23. },
    24. {
    25. id:"003",
    26. name:"深入理解计算机组成原理",
    27. price:3
    28. },
    29. {
    30. id:"004",
    31. name:"现代操作系统",
    32. price:4
    33. },
    34. ]
    35. },
    36. computed:{
    37. totalPirce(){
    38. let totalCount = 0
    39. // 第一种写法
    40. // this.books.map(item=>{
    41. // totalCount=Number(totalCount)+Number(item.price);
    42. // });
    43. // 第二种 原始写法
    44. // for(let i = 0;i
    45. // totalCount=Number(totalCount)+Number(this.books[i].price);
    46. // }
    47. // es6 for循环 in
    48. // for(let i in this.books){
    49. // totalCount=Number(totalCount)+Number(this.books[i].price);
    50. // }
    51. // es6 for循环 of
    52. for(let book of this.books){
    53. totalCount=Number(totalCount)+Number(book.price);
    54. }
    55. return totalCount
    56. }
    57. }
    58. })
    59. script>
    60. body>

    22-(了解)课堂回顾

    1、邂逅vue.js:

    认识vue, vue的读音,为啥学vue,  什么是渐进式,vue特点, 

    2、安装vue(3中方式)

    3、vue初体验:三个案例:

    • mustache 语法, 体验vue响应式,
    • vue列表展示,v-for 
    • vue计数器小案例  ,事件监听,@click

    4、vue中的mvvm

    5、创建vue  option 可以放那些东西【el, data, methods, lifestyle】

    6、插值语法(mustache,v-html,, v-text, v-once, v-pre, v-cloak)

    7、v-bind 绑定基本属性

    8、v-bind 动态绑定class【对象语法和数组语法】 列表小作业

    9、v-bind 动态绑定style【对象语法和数组语法】

    10、computed 讲解两个案例

  • 相关阅读:
    crm系统哪家好?
    mac安装mysql,php,nginx,phpmyadmin,配置访问数据库
    【51单片机实验笔记】前篇(三) 模块功能封装汇总(持续更新)
    pdf文件大小超过限制怎么办?一招教你压缩pdf文件
    【CQF Finance Class 1 现金时间价值】
    一个混乱千万级软件项目
    前端——Vue响应式适配
    vite + vue3.0 + ts 项目搭建
    好用到哭!没想到听书神器这么适合我~
    go语言有哪些web框架
  • 原文地址:https://blog.csdn.net/qq_37299479/article/details/126518183