• Vue的详细教程--基础语法【上】


    🥳🥳Welcome Huihui's Code World ! !🥳🥳

    接下来看看由辉辉所写的关于Vue的相关操作吧

    目录

    🥳🥳Welcome Huihui's Code World ! !🥳🥳

    一.插值

    1.文本

    2.html

    3.属性&class绑定&style绑定

    4.表达式

    二.指令

    1.v-if&v-else&v-else-if

    2.v-show

    3.v-for

    三.动态参数

    四.过滤器

    1.局部过滤器

    2.全局过滤器

    五.计算属性&监听属性

    1.计算属性

    2.监听属性

     六.购物车案例

    效果

    代码


    一.插值

    1.文本

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>插值title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <h1>文本h1>
    11. {{content1}}
    12. div>
    13. body>
    14. <script type="text/javascript">
    15. // 构建vue实例 绑定边界
    16. new Vue({
    17. el:'#app',
    18. data(){
    19. return{
    20. content1:'我是天才' ,
    21. content2 :'我是天才'
    22. }
    23. }
    24. })
    25. script>
    26. body>
    27. html>

    2.html

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>插值title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <h1>文本h1>
    11. {{content1}}
    12. <h1>htmlh1>
    13. <h2 v-html="content2">h2>
    14. div>
    15. body>
    16. <script type="text/javascript">
    17. // 构建vue实例 绑定边界
    18. new Vue({
    19. el:'#app',
    20. data(){
    21. return{
    22. content1:'我是天才' ,
    23. content2 :'我是天才'
    24. }
    25. },
    26. methods: {
    27. },
    28. })
    29. script>
    30. body>
    31. html>

    注意!!!

    3.属性&class绑定&style绑定

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>插值title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <style>
    10. .fs{
    11. font-size: 38px;
    12. }
    13. style>
    14. <div id="app">
    15. <h1>classh1>
    16. <h2 :class="content3 " v-html="content2">h2>
    17. div>
    18. body>
    19. <script type="text/javascript">
    20. // 构建vue实例 绑定边界
    21. new Vue({
    22. el:'#app',
    23. data(){
    24. return{
    25. content2 :'我是天才' ,
    26. content3 :'fs' ,
    27. }
    28. },
    29. methods: {
    30. },
    31. })
    32. script>
    33. body>
    34. html>

    4.表达式

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>插值title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <style>
    10. .fs{
    11. font-size: 100px;
    12. }
    13. style>
    14. <div id="app">
    15. <h1>表达式h1>
    16. {{content4+2}}br>
    17. {{content5.substr(0,4)}}br>
    18. <input v-model="content6" />
    19. {{content6==1 ?'是的':'不细啊'}}
    20. div>
    21. body>
    22. <script type="text/javascript">
    23. // 构建vue实例 绑定边界
    24. new Vue({
    25. el:'#app',
    26. data(){
    27. return{
    28. content4 :5,
    29. content5 :'我是天才的女人',
    30. content6:1
    31. }
    32. },
    33. methods: {
    34. },
    35. })
    36. script>
    37. body>
    38. html>

    二.指令

    1.v-if&v-else&v-else-if

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>指令title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <h1>v-if...h1>
    11. <input v-model="score"/><br/>
    12. <b v-if="score<60">情商低b>
    13. <b v-else-if="score>60 && score<70">情商忽上忽下b>
    14. <b v-else-if="score>70 && score<80">情商一般b>
    15. <b v-else-if="score>80 && score<90">中等情商b>
    16. <b v-else=" ">高情商b>
    17. div>
    18. body>
    19. <script type="text/javascript">
    20. // 构建vue实例 绑定边界
    21. new Vue({
    22. el:'#app',
    23. data(){
    24. return{
    25. score:69,
    26. }
    27. },
    28. methods: {
    29. },
    30. })
    31. script>
    32. body>
    33. html>

    2.v-show

    3.v-for

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>指令title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <h1>v-if...h1>
    11. <input v-model="score"/><br/>
    12. <b v-if="score<60">情商低b>
    13. <b v-else-if="score>=60 && score<=70">情商忽上忽下b>
    14. <b v-else-if="score>70 && score<=80">情商一般b>
    15. <b v-else-if="score>80 && score<=90">中等情商b>
    16. <b v-else=" ">高情商b>
    17. <h1>v-if和v-show的区别h1>
    18. <b v-if="ifshow">ifb>
    19. <b v-show="ifshow">showb>
    20. <h1>v-forh1>
    21. <i v-for="a in arr">{{a}}  i><br />
    22. <i v-for="d in dogs">{{d.name}}  i><br />
    23. <i v-for="i,d in dogs">{{i}}  i><br />
    24. <select>
    25. <option v-for="h in hobby" :value="h.id">{{h.name}}option>
    26. select><br />
    27. <div v-for="h in hobby">
    28. <input type="checkbox" name="hobby" value="h.id"/>{{h.name}}
    29. div>
    30. div>
    31. body>
    32. <script type="text/javascript">
    33. // 构建vue实例 绑定边界
    34. new Vue({
    35. el:'#app',
    36. data(){
    37. return{
    38. score:69,
    39. ifshow:false,
    40. arr:[1,3,5,7,9],
    41. dogs:[{
    42. id:1,name:'夏威',
    43. },{
    44. id:2,name:'xw',
    45. },{
    46. id:3,name:'小威威',
    47. },{
    48. id:4,name:'夏威夷',
    49. }],
    50. hobby:[{
    51. id:1,name:'打夏威',
    52. },{
    53. id:2,name:'爱xw',
    54. },{
    55. id:3,name:'亲小威威',
    56. },{
    57. id:4,name:'去夏威夷',
    58. }],
    59. }
    60. },
    61. methods: {
    62. },
    63. })
    64. script>
    65. body>
    66. html>

    三.动态参数

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>过滤器title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <div id="app">
    9. <h1>动态参数h1>
    10. <input v-model="evname" />
    11. <button v-on:[evname]="test">clickbutton>
    12. div>
    13. <script type="text/javascript">
    14. // 构建vue实例 绑定边界
    15. new Vue({
    16. el:'#app',
    17. data(){
    18. return{
    19. evname:'click'
    20. };
    21. },
    22. methods: {
    23. test(){
    24. alert("你再点一下!!!!");
    25. }
    26. }
    27. })
    28. script>
    29. html>

    四.过滤器

    1.局部过滤器

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>过滤器title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <div id="app">
    9. <h1>动态参数h1>
    10. <input v-model="evname" />
    11. <button v-on:[evname]="test">clickbutton>
    12. <h1>局部过滤器h1>
    13. {{content}}<br />
    14. {{content | filter1}}<br />
    15. <hr />
    16. {{content}}<br />
    17. {{content | filter1 | filter2}}<br />
    18. <h1>过滤器传参h1>
    19. {{content | filter3(4,9)}}<br />
    20. <hr />
    21. div>
    22. <script type="text/javascript">
    23. // 构建vue实例 绑定边界
    24. new Vue({
    25. el:'#app',
    26. filters:{
    27. 'filter1':function(f){
    28. return f.substring(0,6);
    29. },
    30. 'filter2':function(f){
    31. return f.substring(0,4);
    32. },
    33. 'filter3':function(f,s,e){
    34. return f.substring(s,e);
    35. }
    36. },
    37. data(){
    38. return{
    39. content:'红红火火恍恍惚惚',
    40. time:new Date()
    41. };
    42. },
    43. methods: {
    44. test(){
    45. alert("你再点一下!!!!");
    46. }
    47. }
    48. })
    49. script>
    50. html>

    2.全局过滤器

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>过滤器title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. <script src="js/date.js" type="text/javascript" >script>
    8. head>
    9. <div id="app">
    10. <h1>动态参数h1>
    11. <input v-model="evname" />
    12. <button v-on:[evname]="test">clickbutton>
    13. <h1>全局过滤器h1>
    14. {{time}}<br />
    15. {{time | all }}
    16. div>
    17. <script type="text/javascript">
    18. // 全局过滤器
    19. Vue.filter('all', function(value) {
    20. return fmtDate(value);
    21. });
    22. // 构建vue实例 绑定边界
    23. new Vue({
    24. el:'#app',
    25. data(){
    26. return{
    27. content:'红红火火恍恍惚惚',
    28. time:new Date()
    29. };
    30. },
    31. methods: {
    32. test(){
    33. alert("你再点一下!!!!");
    34. }
    35. }
    36. })
    37. script>
    38. html>

    五.计算属性&监听属性

    1.计算属性

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>计算属性&监听属性title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <h1>计算属性h1>
    11. 单价:<input v-model="price"/>
    12. 数量:<input v-model="num"/>
    13. 小计:{{sum}}
    14. div>
    15. <script type="text/javascript">
    16. // 构建vue实例 绑定边界
    17. new Vue({
    18. el:'#app',
    19. data(){
    20. return{
    21. price:79,
    22. num:10,
    23. }
    24. },
    25. computed:{
    26. sum:function(){
    27. return this.price * this.num
    28. }
    29. },
    30. methods: {
    31. },
    32. })
    33. script>
    34. body>
    35. html>

    2.监听属性

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>计算属性&监听属性title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <h1>监听属性h1>
    11. 千米:<input v-model="km"/>
    12. 米:<input v-model="m"/>
    13. div>
    14. <script type="text/javascript">
    15. // 构建vue实例 绑定边界
    16. new Vue({
    17. el:'#app',
    18. data(){
    19. return{
    20. m:1000,
    21. km:1
    22. }
    23. },
    24. watch:{
    25. km:function(v){
    26. //v是千米
    27. this.m=parseInt(v)*1000
    28. },
    29. m:function(v){
    30. //v是米
    31. this.km=parseInt(v)/1000
    32. }
    33. },
    34. methods: {
    35. },
    36. })
    37. script>
    38. body>
    39. html>

     六.购物车案例

    效果

    代码

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js">script>
    6. <title>购物车title>
    7. <style>
    8. .cart-title {
    9. color: white;
    10. font-size: 24px;
    11. margin-bottom: 20px;
    12. }
    13. .cart-items {
    14. display: grid;
    15. grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    16. gap: 20px;
    17. }
    18. .cart-item {
    19. background-color: white;
    20. padding: 10px;
    21. border-radius: 5px;
    22. }
    23. .item-image {
    24. width: 100%;
    25. height: auto;
    26. }
    27. .item-name {
    28. margin-top: 10px;
    29. font-size: 18px;
    30. }
    31. .item-price {
    32. font-size: 16px;
    33. color: grey;
    34. }
    35. .item-quantity {
    36. display: flex;
    37. align-items: center;
    38. }
    39. .quantity-label {
    40. margin-right: 5px;
    41. }
    42. .quantity-button {
    43. cursor: pointer;
    44. background-color: #eee;
    45. padding: 5px;
    46. border: none;
    47. border-radius: 3px;
    48. }
    49. .subtotal {
    50. font-size: 16px;
    51. color: grey;
    52. margin-top: 5px;
    53. }
    54. .total {
    55. font-size: 20px;
    56. margin-top: 10px;
    57. }
    58. th, td {
    59. padding: 8px;
    60. text-align: center;
    61. }
    62. style>
    63. head>
    64. <body>
    65. <div id="app">
    66. <div class="cart-container">
    67. <h1 class="cart-title">购物车h1>
    68. <table width="100%" border="0;" cellspacing="0" cellpadding="0">
    69. <thead>
    70. <tr>
    71. <th>商品th>
    72. <th>数量th>
    73. <th>单价th>
    74. <th>小计th>
    75. tr>
    76. thead>
    77. <tbody>
    78. <tr v-for="item in items" :key="item.id">
    79. <td >
    80. <img :src="item.image" style="width: 64px; height: 64px;" alt="商品图片">
    81. <h3 class="item-name">{{ item.name }}h3>
    82. td>
    83. <td class="item-quantity">
    84. <button @click="updateQuantity(item, -1)">-button>
    85. <span class="quantity-label">{{ item.quantity }}span>
    86. <button @click="updateQuantity(item, 1)">+button>
    87. td>
    88. <td class="item-price">{{ item.price }}td>
    89. <td>
    90. <p class="subtotal">{{ calculateSubtotal(item) }}p>
    91. td>
    92. tr>
    93. tbody>
    94. <tfoot>
    95. <tr>
    96. <td colspan="3">td>
    97. <td>
    98. <p class="total" colspan="4" sty>总计{{ calculateTotal() }}p>
    99. td>
    100. tr>
    101. tfoot>
    102. table>
    103. div>
    104. div>
    105. <script>
    106. new Vue({
    107. el: '#app',
    108. data: {
    109. items: [
    110. { id: 1, name: '商品1', price: '¥199', image: 'img/美妆类3.jpg ', quantity: 1 },
    111. { id: 2, name: '商品2', price: '¥299', image: 'img/美妆类5.jpg', quantity: 1 },
    112. { id: 3, name: '商品3', price: '¥399', image: 'img/美妆类7.jpg', quantity: 1 },
    113. ]
    114. },
    115. methods: {
    116. updateQuantity(item, amount) {
    117. item.quantity += amount;
    118. },
    119. calculateSubtotal(item) {
    120. return '¥' + (parseFloat(item.price.slice(1)) * item.quantity).toFixed(2);
    121. },
    122. calculateTotal() {
    123. let total = 0;
    124. for (let item of this.items) {
    125. total += parseFloat(item.price.slice(1)) * item.quantity;
    126. }
    127. return '¥' + total.toFixed(2);
    128. }
    129. }
    130. });
    131. script>
    132. body>
    133. html>

     好啦,今天的分享就到这了,希望能够帮到你呢!😊😊 

  • 相关阅读:
    Spring Cloud微服务:Loadbalancer 实战
    Maven解决静态资源过滤问题
    天黑了、让我为你关窗帘吧
    Spring Boot 常用注解详解:全面指南
    Dirac delta function (狄拉克 delta 函数)
    【Linux成长史】Linux基本指令大全
    通讯录多版本代码归纳
    Elastic Search6.8.0安装
    C语言中realloc函数解析
    Maven打包错误:Please refer to XXXXX for the individual test results._zhizhiqiuya
  • 原文地址:https://blog.csdn.net/m0_74315688/article/details/132993745