• 看看Vue


    Vue基础

    Vue简介

    1. 渐进式JavaScript框架
    2. 简化Dom操作
    3. 响应式数据驱动

    第一个Vue

    1. 导入开发版本的Vue.js
    2. 创建Vue实例对象,设置el属性和data属性
    3. 使用简洁的的模板语法把数据渲染到页面上

    el挂载点

    Vue实例的作用范围是什么?

    在el命中的元素内部

    是否可以使用其他的选择器?

    id选择器、 class选择器、标签选择器。一般使用id选择器,是唯一的不会命中多个元素。

    是否可以设置其他的dom元素?

    可以使用其他的双标签(比如

     

    ),但不要把id挂载到 body / html 上。

    data数据对象

    1. Vue要用到的数据定义在data中
    2. data中可以写复杂类型的数据
    3. 渲染复杂类型数据时,遵守js的语法即可(对象的点语法,数组类型的索引语法)。

    本地应用-通过Vue实现常见的网页效果,Vue指令

    v-text指令

    作用:设置标签的内容,默认会替换标签中全部内容,使用差值表达式可以替换局部\指定内容。

    v-html指令

    作用:设置innerHTML,内容中有html会被解析为标签。

    v-on指令 

    作用:为元素绑定事件 可以将 'v-on:' 简写成 '@',绑定的方法写在methods属性中。

    v-show指令

    作用:根据真假切换元素的显示状态,其原理是修改元素的display,实现显示、隐藏,指令后面的内容最终都会解析成布尔值,true显示、false隐藏,数据改变之后,对应元素的显示状态对同步更新。

    v-if指令

    作用:根据表达式的真假切换元素的显示状态。本质是通过操作dom元素来切换显示状态。表达式的值为true,元素存在于dom树中,为false,从dom中移除。频繁的切换使用v-show,反之使用v-if,v-show切换消耗小。

    v-model

    作用:便捷的设置和获取表单元素的值,绑定的数据会和表单元素值相关联,双向关联。

    MVVM模型

    M:模型(Model):data中的数据

    V:视图(View):模板代码

    VM:视图模型(ViewModel):Vue实例

    data中所有的属性,最后都出现在了vm身上。

    vm身上所有的属性及Vue原型上所有属性,在Vue模板中都可以直接使用。

    记事本Demo

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <script src="../JS/vue.js">script>
    9. head>
    10. <body>
    11. <section id="todoapp">
    12. <header class="header">
    13. <h1>记事本h1>
    14. <input v-model="inputValue" @keyup.enter="add" autofocus="autofocus" autocomplete="off" placeholder="请输入"
    15. class="new-todo">
    16. header>
    17. <section class="main">
    18. <ul class="todo-list">
    19. <li class="todo" v-for="(item,index) in list">
    20. <div class="view">
    21. <span class="index">{{ index+1 }}.span>
    22. <label>{{ item }}label>
    23. <button class="destory" @click="remove(index)">button>
    24. div>
    25. li>
    26. ul>
    27. section>
    28. <footer class="footer">
    29. <span class="todo-count"> <strong>{{ list.length }}strong> items left span>
    30. <button class="clear-completed" @click="clear">Clearbutton>
    31. footer>
    32. section>
    33. <script>
    34. // 创建Vue对象
    35. var app = new Vue({
    36. // 挂载容器todoapp
    37. el: "#todoapp",
    38. // 定义一个数组,初始值
    39. data: {
    40. list: ["小行星", "放放风"],
    41. inputValue: "好好学习"
    42. },
    43. methods: {
    44. add: function () {
    45. //向数组中添加push输入的值
    46. this.list.push(this.inputValue);
    47. },
    48. remove: function (index) {
    49. //根据索引删除1行
    50. this.list.splice(index, 1);
    51. },
    52. clear: function () {
    53. //将数组置为空
    54. this.list = [];
    55. }
    56. }
    57. })
    58. script>
    59. body>
    60. html>

    效果

    实现新增,删除 ,计数,清空

    天气预报Demo

    页面

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    7. <title>天知道title>
    8. <script src="../JS/vue.js">script>
    9. <link rel="stylesheet" href="css/reset.css" />
    10. <link rel="stylesheet" href="css/index.css" />
    11. head>
    12. <body>
    13. <div class="wrap" id="app">
    14. <div class="search_form">
    15. <div class="logo"><img src="img/logo.png" alt="logo" />div>
    16. <div class="form_group">
    17. <input type="text" class="input_txt" placeholder="请输入查询的天气" v-model="city" @keyup.enter="queryWeather" />
    18. <button class="input_sub" @click="queryWeather">
    19. 搜 索
    20. button>
    21. div>
    22. <div class="hotkey">
    23. <a href="javascript:;" v-for="city in hotCitys" @click="clickSearch(city)">{{ city }}a>
    24. div>
    25. div>
    26. <ul class="weather_list">
    27. <li v-for="(item,index) in forecastList" :key="item.date" :style="{transitionDelay:index*100+'ms'}">
    28. <div class="info_type">
    29. <span class="iconfont">{{ item.type }}span>
    30. div>
    31. <div class="info_temp">
    32. <b>{{ item.low}}b>
    33. ~
    34. <b>{{ item.high}}b>
    35. div>
    36. <div class="info_date">
    37. <span>{{ item.date }}span>
    38. div>
    39. li>
    40. ul>
    41. div>
    42. <script src="https://unpkg.com/axios/dist/axios.min.js">script>
    43. <script>
    44. new Vue({
    45. el: "#app",
    46. data: {
    47. city: "武汉",
    48. forecastList: [],
    49. hotCitys: ["北京", "上海", "广州", "深圳"]
    50. },
    51. methods: {
    52. queryWeather() {
    53. this.forecastList = [];
    54. axios
    55. // 使用get方式传参
    56. .get(`http://wthrcdn.etouch.cn/weather_mini?city=${this.city}`)
    57. .then(res => {
    58. console.log(res);
    59. // 拿到forecast中的数据
    60. this.forecastList = res.data.data.forecast;
    61. })
    62. .catch(err => {
    63. console.log(err);
    64. })
    65. .finally(() => { });
    66. },
    67. clickSearch(city) {
    68. this.city = city;
    69. this.queryWeather();
    70. }
    71. }
    72. });
    73. script>
    74. body>
    75. html>

    样式CSS

    1. body{
    2. font-family:'Microsoft YaHei';
    3. }
    4. .wrap{
    5. position: fixed;
    6. left:0;
    7. top:0;
    8. width:100%;
    9. height:100%;
    10. /* background: radial-gradient(#f3fbfe, #e4f5fd, #8fd5f4); */
    11. /* background:#8fd5f4; */
    12. /* background: linear-gradient(#6bc6ee, #fff); */
    13. background:#fff;
    14. }
    15. .search_form{
    16. width:640px;
    17. margin:100px auto 0;
    18. }
    19. .logo img{
    20. display:block;
    21. margin:0 auto;
    22. }
    23. .form_group{
    24. width:640px;
    25. height:40px;
    26. margin-top:45px;
    27. }
    28. .input_txt{
    29. width:538px;
    30. height:38px;
    31. padding:0px;
    32. float:left;
    33. border:1px solid #41a1cb;
    34. outline:none;
    35. text-indent:10px;
    36. }
    37. .input_sub{
    38. width:100px;
    39. height:40px;
    40. border:0px;
    41. float: left;
    42. background-color: #41a1cb;
    43. color:#fff;
    44. font-size:16px;
    45. outline:none;
    46. cursor: pointer;
    47. position: relative;
    48. }
    49. .input_sub.loading::before{
    50. content:'';
    51. position: absolute;
    52. left: 0;
    53. top: 0;
    54. width: 100%;
    55. height: 100%;
    56. background: url('../img/loading.gif');
    57. }
    58. .hotkey{
    59. margin:3px 0 0 2px;
    60. }
    61. .hotkey a{
    62. font-size:14px;
    63. color:#666;
    64. padding-right:15px;
    65. }
    66. .weather_list{
    67. height:200px;
    68. text-align:center;
    69. margin-top:50px;
    70. font-size:0px;
    71. }
    72. .weather_list li{
    73. display:inline-block;
    74. width:140px;
    75. height:200px;
    76. padding:0 10px;
    77. overflow: hidden;
    78. position: relative;
    79. background:url('../img/line.png') right center no-repeat;
    80. background-size: 1px 130px;
    81. }
    82. .weather_list li:last-child{
    83. background:none;
    84. }
    85. .info_date{
    86. width:100%;
    87. height:40px;
    88. line-height:40px;
    89. color:#999;
    90. font-size:14px;
    91. left:0px;
    92. bottom:0px;
    93. margin-top: 15px;
    94. }
    95. .info_date b{
    96. float: left;
    97. margin-left:15px;
    98. }
    99. .info_type span{
    100. color:#fda252;
    101. font-size:30px;
    102. line-height:80px;
    103. }
    104. .info_temp{
    105. font-size:14px;
    106. color:#fda252;
    107. }
    108. .info_temp b{
    109. font-size:13px;
    110. }
    111. .tem .iconfont {
    112. font-size: 50px;
    113. }
    1. body,ul,h1,h2,h3,h4,h5,h6{
    2. margin: 0;
    3. padding: 0;
    4. }
    5. h1,h2,h3,h4,h5,h6{
    6. font-size:100%;
    7. font-weight:normal;
    8. }
    9. a{
    10. text-decoration:none;
    11. }
    12. ul{
    13. list-style:none;
    14. }
    15. img{
    16. border:0px;
    17. }
    18. /* 清除浮动,解决margin-top塌陷 */
    19. .clearfix:before,.clearfix:after{
    20. content:'';
    21. display:table;
    22. }
    23. .clearfix:after{
    24. clear:both;
    25. }
    26. .clearfix{
    27. zoom:1;
    28. }
    29. .fl{
    30. float:left;
    31. }
    32. .fr{
    33. float:right;
    34. }

    效果

    实现点击搜索查询,回车查询

  • 相关阅读:
    深度学习零基础教程
    LINUX下看门狗的使用
    rh358 002 fact变量获取 ansible配置网络 service_facts
    QT+OSG/osgEarth编译之十八:geos+Qt编译(一套代码、一套框架,跨平台编译,版本:geos-3.11.0)
    【32-业务开发-基础业务-规格参数-保存数据-查询数据-更新操作之数据回显展示-更新操作-前后端项目交互整合与测试-总结收获】
    Head First设计模式中的典型设计模式解析与案例分析
    基于工程车辆/物流车辆/消防车辆远程通信的车队管理解决方案
    【kubernetes】使用virtual-kubelet扩展k8s
    ubuntu20.04桌面蓝屏问题解决
    TS中null和undefined特殊性
  • 原文地址:https://blog.csdn.net/Ipkiss_Yongheng/article/details/126465315