• Vue基础与常用指令,Element基础


    1.vue快速入门

    vue概述

    Vue是一套构建用户界面的渐进式前端框架

    只关注视图层,并且非常容易学习,还可以很方便的与其它库或已有项目整合

    通过尽可能简单的API来实现响应数据的绑定和组合的视图组件

    特点

    易用:在有HTMLCSSJavaScript的基础上,快速上手。

    灵活:简单小巧的核心,渐进式技术栈,足以应付任何规模的应用。

    性能:20kbmin+gzip运行大小、超快虚拟DOM、最省心的优化

    快速入门

    1.下载和引入vue.js文件

    2.编写入门程序

            视图:负责页面渲染,主要由HTML+CSS构成

            脚本:负责业务数据模型(Model)以及数据的处理逻辑

    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. <title>快速入门title>
    7. head>
    8. <body>
    9. <div id="div">
    10. {{msg}}
    11. div>
    12. body>
    13. <script src="vue.js">script>
    14. <script>
    15. //脚本
    16. new Vue({
    17. el:"#div",
    18. data:{
    19. msg:"Hello Vue"
    20. }
    21. });
    22. script>
    23. html>

    Vue 核心对象:每一个 Vue 程序都是从一个 Vue 核心对象开始的

    1. let vm = new Vue({
    2. 选项列表;
    3. });

    选项列表

    el选项:用于接收获取到页面中的元素。(根据常用选择器获取)

    data选项:用于保存当前Vue对象中的数据。在视图中声明的变量需要在此处赋值

    methods选项:用于定义方法。方法可以直接通过对象名调用,this代表当前Vue对象 

    数据绑定

    在视图部分获取脚本部分的数据。

    {{变量名}}

    快速入门升级

    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. <title>快速入门升级title>
    7. head>
    8. <body>
    9. <div id="div">
    10. <div>姓名:{{name}}div>
    11. <div>班级:{{classRoom}}div>
    12. <button onclick="hi()">打招呼button>
    13. <button onclick="update()">修改班级button>
    14. div>
    15. body>
    16. <script src="vue.js">script>
    17. <script>
    18. // 脚本
    19. let vm = new Vue({
    20. el:"#div",
    21. data:{
    22. name:"张三",
    23. classRoom:"一班"
    24. },
    25. methods:{
    26. study(){
    27. alert(this.name + "正在" + this.classRoom + "好好学习!");
    28. }
    29. }
    30. });
    31. //定义打招呼方法
    32. function hi(){
    33. vm.study();
    34. }
    35. //定义修改班级
    36. function update(){
    37. vm.classRoom = "二班";
    38. }
    39. script>
    40. html>

    2.vue常用指令

    1.指令的介绍

    指令:是带有 v- 前缀的特殊属性,不同指令具有不同含义。例如 v-html,v-if,v-for

    使用指令时,通常编写在标签的属性上,值可以使用 JS 的表达式

    指令:vue框架定义的,一些标签的自定义的属性

    常用指令

    指令作用
    v-html把文本解析为HTML代码
    v-bind为HTML标签绑定属性值
    v-if条件性的渲染某元素,判定为true时渲染,否则不渲染
    v-else
    v-else-if
    v-show根据条件展示某元素,区别在于切换的是display属性的值
    v-for列表渲染,遍历容器的元素或者对象的属性
    v-on为HTML标签绑定事件
    v-model为表单元素上创建双向数据绑定

    2.文本插值

    v-html:把文本解析为 HTML 代码 

    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. <title>文本插值title>
    7. head>
    8. <body>
    9. <div id="div">
    10. <div>{{msg}}div>
    11. <div v-html="msg">div>
    12. div>
    13. body>
    14. <script src="vue.js">script>
    15. <script>
    16. new Vue({
    17. el:"#div",
    18. data:{
    19. msg:"Hello Vue"
    20. }
    21. });
    22. script>
    23. html>

    3.绑定属性

    v-bind:为 HTML 标签绑定属性值 

    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. <title>绑定属性title>
    7. <style>
    8. .my{
    9. border: 1px solid red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div id="div">
    15. <a v-bind:href="url">百度一下a>
    16. <br>
    17. <a :href="url">百度一下a>
    18. <br>
    19. <div :class="cls">我是divdiv>
    20. div>
    21. body>
    22. <script src="vue.js">script>
    23. <script>
    24. new Vue({
    25. el:"#div",
    26. data:{
    27. url:"https://www.baidu.com",
    28. cls:"my"
    29. }
    30. });
    31. script>
    32. html>

    4.条件渲染 

    v-if:条件性的渲染某元素,判定为真时渲染,否则不渲染

    v-else:条件性的渲染

    v-else-if:条件性的渲染

    v-show:根据条件展示某元素,区别在于切换的是display属性的值

    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. <title>条件渲染title>
    7. head>
    8. <body>
    9. <div id="div">
    10. <div v-if="num % 3 == 0">div1div>
    11. <div v-else-if="num % 3 == 1">div2div>
    12. <div v-else="num % 3 == 2">div3div>
    13. <div v-show="flag">div4div>
    14. div>
    15. body>
    16. <script src="vue.js">script>
    17. <script>
    18. new Vue({
    19. el:"#div",
    20. data:{
    21. num:1,
    22. flag:false
    23. }
    24. });
    25. script>
    26. html>

    5.列表渲染 

    v-for:列表渲染,遍历容器的元素或者对象的属性

    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. <title>列表渲染title>
    7. head>
    8. <body>
    9. <div id="div">
    10. <ul>
    11. <li v-for="name in names">
    12. {{name}}
    13. li>
    14. <li v-for="value in student">
    15. {{value}}
    16. li>
    17. ul>
    18. div>
    19. body>
    20. <script src="vue.js">script>
    21. <script>
    22. new Vue({
    23. el:"#div",
    24. data:{
    25. names:["张三","李四","王五"],
    26. student:{
    27. name:"张三",
    28. age:23
    29. }
    30. }
    31. });
    32. script>
    33. html>

    6.事件绑定 

    v-on:为 HTML 标签绑定事件

    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. <title>事件绑定title>
    7. head>
    8. <body>
    9. <div id="div">
    10. <div>{{name}}div>
    11. <button v-on:click="change()">改变div的内容button>
    12. <button v-on:dblclick="change()">改变div的内容button>
    13. <button @click="change()">改变div的内容button>
    14. div>
    15. body>
    16. <script src="vue.js">script>
    17. <script>
    18. new Vue({
    19. el:"#div",
    20. data:{
    21. name:"周杰伦"
    22. },
    23. methods:{
    24. change(){
    25. this.name = "王力宏"
    26. }
    27. }
    28. });
    29. script>
    30. html>

    7.表单绑定

    表单绑定

    v-model:在表单元素上创建双向数据绑定

    双向数据绑定

    更新data数据,页面中的数据也会更新

    更新页面数据,data数据也会更新

    MVVM模型(ModelViewViewModel):是MVC模式的改进版

    在前端页面中,JS对象表示Model,页面表示View,两者做到了最大限度的分离

    将Model和View关联起来的就是ViewModel,它是桥梁

    ViewModel负责把Model的数据同步到View显示出来,还负责把View修改的数据同步回Model

    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. <title>表单绑定title>
    7. head>
    8. <body>
    9. <div id="div">
    10. <form autocomplete="off">
    11. 姓名:<input type="text" name="username" v-model="username">
    12. <br>
    13. 年龄:<input type="number" name="age" v-model="age">
    14. form>
    15. div>
    16. body>
    17. <script src="vue.js">script>
    18. <script>
    19. new Vue({
    20. el:"#div",
    21. data:{
    22. username:"张三",
    23. age:23
    24. }
    25. });
    26. script>
    27. html>

     3.Element基本使用

    1.Element概述

    Element:网站快速成型工具。是饿了么公司前端开发团队提供的一套基于Vue的网站组件库。

    使用Element前提必须要有Vue。

    组件:组成网页的部件,例如超链接、按钮、图片、表格等等

    Element官网:https://element.eleme.cn/#/zh-CN

    2.Element快速入门

    1.下载 Element 核心库。

    2.引入 Element 样式文件。

    3.引入 Vue 核心 js 文件。

    4.引入 Element 核心 js 文件。

    5.编写按钮标签。

    6.通过 Vue 核心对象加载元素

    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. <title>快速入门title>
    7. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    8. <script src="vue.js">script>
    9. <script src="element-ui/lib/index.js">script>
    10. head>
    11. <body>
    12. <button>我是按钮button>
    13. <br>
    14. <div id="div">
    15. <el-row>
    16. <el-button>默认按钮el-button>
    17. <el-button type="primary">主要按钮el-button>
    18. <el-button type="success">成功按钮el-button>
    19. <el-button type="info">信息按钮el-button>
    20. <el-button type="warning">警告按钮el-button>
    21. <el-button type="danger">危险按钮el-button>
    22. el-row>
    23. <br>
    24. <el-row>
    25. <el-button plain>朴素按钮el-button>
    26. <el-button type="primary" plain>主要按钮el-button>
    27. <el-button type="success" plain>成功按钮el-button>
    28. <el-button type="info" plain>信息按钮el-button>
    29. <el-button type="warning" plain>警告按钮el-button>
    30. <el-button type="danger" plain>危险按钮el-button>
    31. el-row>
    32. <br>
    33. <el-row>
    34. <el-button round>圆角按钮el-button>
    35. <el-button type="primary" round>主要按钮el-button>
    36. <el-button type="success" round>成功按钮el-button>
    37. <el-button type="info" round>信息按钮el-button>
    38. <el-button type="warning" round>警告按钮el-button>
    39. <el-button type="danger" round>危险按钮el-button>
    40. el-row>
    41. <br>
    42. <el-row>
    43. <el-button icon="el-icon-search" circle>el-button>
    44. <el-button type="primary" icon="el-icon-edit" circle>el-button>
    45. <el-button type="success" icon="el-icon-check" circle>el-button>
    46. <el-button type="info" icon="el-icon-message" circle>el-button>
    47. <el-button type="warning" icon="el-icon-star-off" circle>el-button>
    48. <el-button type="danger" icon="el-icon-delete" circle>el-button>
    49. el-row>
    50. div>
    51. body>
    52. <script>
    53. new Vue({
    54. el:"#div"
    55. });
    56. script>
    57. html>

    3.基础布局

    将页面分成最多 24 个部分,自由切分

    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. <title>基础布局title>
    7. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    8. <script src="vue.js">script>
    9. <script src="element-ui/lib/index.js">script>
    10. <style>
    11. .el-row {
    12. /* 行距为20px */
    13. margin-bottom: 20px;
    14. }
    15. .bg-purple-dark {
    16. background: red;
    17. }
    18. .bg-purple {
    19. background: blue;
    20. }
    21. .bg-purple-light {
    22. background: green;
    23. }
    24. .grid-content {
    25. /* 边框圆润度 */
    26. border-radius: 4px;
    27. /* 行高为36px */
    28. min-height: 36px;
    29. }
    30. style>
    31. head>
    32. <body>
    33. <div id="div">
    34. <el-row>
    35. <el-col :span="24"><div class="grid-content bg-purple-dark">div>el-col>
    36. el-row>
    37. <el-row>
    38. <el-col :span="12"><div class="grid-content bg-purple">div>el-col>
    39. <el-col :span="12"><div class="grid-content bg-purple-light">div>el-col>
    40. el-row>
    41. <el-row>
    42. <el-col :span="8"><div class="grid-content bg-purple">div>el-col>
    43. <el-col :span="8"><div class="grid-content bg-purple-light">div>el-col>
    44. <el-col :span="8"><div class="grid-content bg-purple">div>el-col>
    45. el-row>
    46. <el-row>
    47. <el-col :span="6"><div class="grid-content bg-purple">div>el-col>
    48. <el-col :span="6"><div class="grid-content bg-purple-light">div>el-col>
    49. <el-col :span="6"><div class="grid-content bg-purple">div>el-col>
    50. <el-col :span="6"><div class="grid-content bg-purple-light">div>el-col>
    51. el-row>
    52. <el-row>
    53. <el-col :span="4"><div class="grid-content bg-purple">div>el-col>
    54. <el-col :span="4"><div class="grid-content bg-purple-light">div>el-col>
    55. <el-col :span="4"><div class="grid-content bg-purple">div>el-col>
    56. <el-col :span="4"><div class="grid-content bg-purple-light">div>el-col>
    57. <el-col :span="4"><div class="grid-content bg-purple">div>el-col>
    58. <el-col :span="4"><div class="grid-content bg-purple-light">div>el-col>
    59. el-row>
    60. div>
    61. body>
    62. <script>
    63. new Vue({
    64. el:"#div"
    65. });
    66. script>
    67. html>

    4.容器布局

    将页面分成头部区域、侧边栏区域、主区域、底部区域 

    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. <title>容器布局title>
    7. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    8. <script src="vue.js">script>
    9. <script src="element-ui/lib/index.js">script>
    10. <style>
    11. .el-header, .el-footer {
    12. background-color: #d18e66;
    13. color: #333;
    14. text-align: center;
    15. height: 100px;
    16. }
    17. .el-aside {
    18. background-color: #55e658;
    19. color: #333;
    20. text-align: center;
    21. height: 580px;
    22. }
    23. .el-main {
    24. background-color: #5fb1f3;
    25. color: #333;
    26. text-align: center;
    27. height: 520px;
    28. }
    29. style>
    30. head>
    31. <body>
    32. <div id="div">
    33. <el-container>
    34. <el-header>头部区域el-header>
    35. <el-container>
    36. <el-aside width="200px">侧边栏区域el-aside>
    37. <el-container>
    38. <el-main>主区域el-main>
    39. <el-footer>底部区域el-footer>
    40. el-container>
    41. el-container>
    42. el-container>
    43. div>
    44. body>
    45. <script>
    46. new Vue({
    47. el:"#div"
    48. });
    49. script>
    50. html>

    5.表单组件

    表单:由输入框、下拉列表、单选框、多选框等控件组成,用以收集、校验、提交数据 

    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. <title>表单组件title>
    7. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    8. <script src="vue.js">script>
    9. <script src="element-ui/lib/index.js">script>
    10. head>
    11. <body>
    12. <div id="div">
    13. <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    14. <el-form-item label="活动名称" prop="name">
    15. <el-input v-model="ruleForm.name">el-input>
    16. el-form-item>
    17. <el-form-item label="活动区域" prop="region">
    18. <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
    19. <el-option label="区域一" value="shanghai">el-option>
    20. <el-option label="区域二" value="beijing">el-option>
    21. el-select>
    22. el-form-item>
    23. <el-form-item label="活动时间" required>
    24. <el-col :span="11">
    25. <el-form-item prop="date1">
    26. <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;">el-date-picker>
    27. el-form-item>
    28. el-col>
    29. <el-col class="line" :span="2">-el-col>
    30. <el-col :span="11">
    31. <el-form-item prop="date2">
    32. <el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;">el-time-picker>
    33. el-form-item>
    34. el-col>
    35. el-form-item>
    36. <el-form-item label="即时配送" prop="delivery">
    37. <el-switch v-model="ruleForm.delivery">el-switch>
    38. el-form-item>
    39. <el-form-item label="活动性质" prop="type">
    40. <el-checkbox-group v-model="ruleForm.type">
    41. <el-checkbox label="美食/餐厅线上活动" name="type">el-checkbox>
    42. <el-checkbox label="地推活动" name="type">el-checkbox>
    43. <el-checkbox label="线下主题活动" name="type">el-checkbox>
    44. <el-checkbox label="单纯品牌曝光" name="type">el-checkbox>
    45. el-checkbox-group>
    46. el-form-item>
    47. <el-form-item label="特殊资源" prop="resource">
    48. <el-radio-group v-model="ruleForm.resource">
    49. <el-radio label="线上品牌商赞助">el-radio>
    50. <el-radio label="线下场地免费">el-radio>
    51. el-radio-group>
    52. el-form-item>
    53. <el-form-item label="活动形式" prop="desc">
    54. <el-input type="textarea" v-model="ruleForm.desc">el-input>
    55. el-form-item>
    56. <el-form-item>
    57. <el-button type="primary" @click="submitForm('ruleForm')">立即创建el-button>
    58. <el-button @click="resetForm('ruleForm')">重置el-button>
    59. el-form-item>
    60. el-form>
    61. div>
    62. body>
    63. <script>
    64. new Vue({
    65. el:"#div",
    66. data:{
    67. ruleForm: {
    68. name: '',
    69. region: '',
    70. date1: '',
    71. date2: '',
    72. delivery: false,
    73. type: [],
    74. resource: '',
    75. desc: ''
    76. },
    77. rules: {
    78. name: [
    79. { required: true, message: '请输入活动名称', trigger: 'blur' },
    80. { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
    81. ],
    82. region: [
    83. { required: true, message: '请选择活动区域', trigger: 'change' }
    84. ],
    85. date1: [
    86. { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
    87. ],
    88. date2: [
    89. { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
    90. ],
    91. type: [
    92. { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
    93. ],
    94. resource: [
    95. { required: true, message: '请选择活动资源', trigger: 'change' }
    96. ],
    97. desc: [
    98. { required: true, message: '请填写活动形式', trigger: 'blur' }
    99. ]
    100. }
    101. },
    102. methods:{
    103. submitForm(formName) {
    104. this.$refs[formName].validate((valid) => {
    105. if (valid) {
    106. alert('submit!');
    107. } else {
    108. console.log('error submit!!');
    109. return false;
    110. }
    111. });
    112. },
    113. resetForm(formName) {
    114. this.$refs[formName].resetFields();
    115. }
    116. }
    117. });
    118. script>
    119. html>

    6.表格组件

    表格:用于展示多条结构类似的数据,可对数据进行编辑、删除或其他自定义操作

    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. <title>表格组件title>
    7. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    8. <script src="vue.js">script>
    9. <script src="element-ui/lib/index.js">script>
    10. head>
    11. <body>
    12. <div id="div">
    13. <template>
    14. <el-table
    15. :data="tableData"
    16. style="width: 100%">
    17. <el-table-column
    18. prop="date"
    19. label="日期"
    20. width="180">
    21. el-table-column>
    22. <el-table-column
    23. prop="name"
    24. label="姓名"
    25. width="180">
    26. el-table-column>
    27. <el-table-column
    28. prop="address"
    29. label="地址">
    30. el-table-column>
    31. <el-table-column
    32. label="操作"
    33. width="180">
    34. <el-button type="warning">编辑el-button>
    35. <el-button type="danger">删除el-button>
    36. el-table-column>
    37. el-table>
    38. template>
    39. div>
    40. body>
    41. <script>
    42. new Vue({
    43. el:"#div",
    44. data:{
    45. tableData: [{
    46. date: '2016-05-02',
    47. name: '王小虎',
    48. address: '上海市普陀区金沙江路 1518 弄'
    49. }, {
    50. date: '2016-05-04',
    51. name: '王小虎',
    52. address: '上海市普陀区金沙江路 1517 弄'
    53. }, {
    54. date: '2016-05-01',
    55. name: '王小虎',
    56. address: '上海市普陀区金沙江路 1519 弄'
    57. }, {
    58. date: '2016-05-03',
    59. name: '王小虎',
    60. address: '上海市普陀区金沙江路 1516 弄'
    61. }]
    62. }
    63. });
    64. script>
    65. html>

    7.顶部导航栏组件

    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. <title>顶部导航栏title>
    7. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    8. <script src="vue.js">script>
    9. <script src="element-ui/lib/index.js">script>
    10. head>
    11. <body>
    12. <div id="div">
    13. <el-menu
    14. :default-active="activeIndex2"
    15. class="el-menu-demo"
    16. mode="horizontal"
    17. @select="handleSelect"
    18. background-color="#545c64"
    19. text-color="#fff"
    20. active-text-color="#ffd04b">
    21. <el-menu-item index="1">处理中心el-menu-item>
    22. <el-submenu index="2">
    23. <template slot="title">我的工作台template>
    24. <el-menu-item index="2-1">选项1el-menu-item>
    25. <el-menu-item index="2-2">选项2el-menu-item>
    26. <el-menu-item index="2-3">选项3el-menu-item>
    27. <el-submenu index="2-4">
    28. <template slot="title">选项4template>
    29. <el-menu-item index="2-4-1">选项1el-menu-item>
    30. <el-menu-item index="2-4-2">选项2el-menu-item>
    31. <el-menu-item index="2-4-3">选项3el-menu-item>
    32. el-submenu>
    33. el-submenu>
    34. <el-menu-item index="3" disabled>消息中心el-menu-item>
    35. <el-menu-item index="4"><a href="https://www.ele.me" target="_blank">订单管理a>el-menu-item>
    36. el-menu>
    37. div>
    38. body>
    39. <script>
    40. new Vue({
    41. el:"#div"
    42. });
    43. script>
    44. html>

    8.侧边导航栏组件

    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. <title>侧边导航栏title>
    7. <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    8. <script src="vue.js">script>
    9. <script src="element-ui/lib/index.js">script>
    10. head>
    11. <body>
    12. <div id="div">
    13. <el-col :span="6">
    14. <el-menu
    15. default-active="2"
    16. class="el-menu-vertical-demo"
    17. @open="handleOpen"
    18. @close="handleClose"
    19. background-color="#545c64"
    20. text-color="#fff"
    21. active-text-color="#ffd04b">
    22. <el-submenu index="1">
    23. <template slot="title">
    24. <i class="el-icon-location">i>
    25. <span>导航一span>
    26. template>
    27. <el-menu-item-group>
    28. <template slot="title">分组一template>
    29. <el-menu-item index="1-1">选项1el-menu-item>
    30. <el-menu-item index="1-2">选项2el-menu-item>
    31. el-menu-item-group>
    32. <el-menu-item-group title="分组2">
    33. <el-menu-item index="1-3">选项3el-menu-item>
    34. el-menu-item-group>
    35. <el-submenu index="1-4">
    36. <template slot="title">选项4template>
    37. <el-menu-item index="1-4-1">选项1el-menu-item>
    38. el-submenu>
    39. el-submenu>
    40. <el-menu-item index="2">
    41. <i class="el-icon-menu">i>
    42. <span slot="title">导航二span>
    43. el-menu-item>
    44. <el-menu-item index="3" disabled>
    45. <i class="el-icon-document">i>
    46. <span slot="title">导航三span>
    47. el-menu-item>
    48. <el-menu-item index="4">
    49. <i class="el-icon-setting">i>
    50. <span slot="title">导航四span>
    51. el-menu-item>
    52. el-menu>
    53. el-col>
    54. div>
    55. body>
    56. <script>
    57. new Vue({
    58. el:"#div"
    59. });
    60. script>
    61. html>

    4.vue高级使用 

    1.自定义组件

    组件其实就是自定义的标签。例如就是对"

  • });
  • new Vue({
  • el:"#div"
  • });
  • script>
  • html>
  • 2.vue的生命周期

    生命周期的八个状态 

    状态阶段周期
    beforeCreate创建前
    created创建后
    beforeMount载入前
    mounted载入后
    beforeUpdate更新前
    updated更新后
    beforeDestory销毁前
    destoryed销毁后
    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. <title>生命周期title>
    7. <script src="vue.js">script>
    8. head>
    9. <body>
    10. <div id="app">
    11. {{message}}
    12. div>
    13. body>
    14. <script>
    15. let vm = new Vue({
    16. el: '#app',
    17. data: {
    18. message: 'Vue的生命周期'
    19. },
    20. beforeCreate: function() {
    21. console.group('------beforeCreate创建前状态------');
    22. console.log("%c%s", "color:red", "el : " + this.$el); //undefined
    23. console.log("%c%s", "color:red", "data : " + this.$data); //undefined
    24. console.log("%c%s", "color:red", "message: " + this.message);//undefined
    25. },
    26. created: function() {
    27. console.group('------created创建完毕状态------');
    28. console.log("%c%s", "color:red", "el : " + this.$el); //undefined
    29. console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化
    30. console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
    31. },
    32. beforeMount: function() {
    33. console.group('------beforeMount挂载前状态------');
    34. console.log("%c%s", "color:red", "el : " + (this.$el)); //已被初始化
    35. console.log(this.$el);
    36. console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化
    37. console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
    38. },
    39. mounted: function() {
    40. console.group('------mounted 挂载结束状态------');
    41. console.log("%c%s", "color:red", "el : " + this.$el); //已被初始化
    42. console.log(this.$el);
    43. console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化
    44. console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
    45. },
    46. beforeUpdate: function() {
    47. console.group('beforeUpdate 更新前状态===============》');
    48. let dom = document.getElementById("app").innerHTML;
    49. console.log(dom);
    50. console.log("%c%s", "color:red", "el : " + this.$el);
    51. console.log(this.$el);
    52. console.log("%c%s", "color:red", "data : " + this.$data);
    53. console.log("%c%s", "color:red", "message: " + this.message);
    54. },
    55. updated: function() {
    56. console.group('updated 更新完成状态===============》');
    57. let dom = document.getElementById("app").innerHTML;
    58. console.log(dom);
    59. console.log("%c%s", "color:red", "el : " + this.$el);
    60. console.log(this.$el);
    61. console.log("%c%s", "color:red", "data : " + this.$data);
    62. console.log("%c%s", "color:red", "message: " + this.message);
    63. },
    64. beforeDestroy: function() {
    65. console.group('beforeDestroy 销毁前状态===============》');
    66. console.log("%c%s", "color:red", "el : " + this.$el);
    67. console.log(this.$el);
    68. console.log("%c%s", "color:red", "data : " + this.$data);
    69. console.log("%c%s", "color:red", "message: " + this.message);
    70. },
    71. destroyed: function() {
    72. console.group('destroyed 销毁完成状态===============》');
    73. console.log("%c%s", "color:red", "el : " + this.$el);
    74. console.log(this.$el);
    75. console.log("%c%s", "color:red", "data : " + this.$data);
    76. console.log("%c%s", "color:red", "message: " + this.message);
    77. }
    78. });
    79. // 销毁Vue对象
    80. //vm.$destroy();
    81. //vm.message = "hehe"; // 销毁后 Vue 实例会解绑所有内容
    82. // 设置data中message数据值
    83. vm.message = "good...";
    84. script>
    85. html>

    3.异步操作

    在Vue中发送异步请求,本质上还是AJAX。我们可以使用axios这个插件来简化操作

    使用步骤

    1.引入axios核心js文件

    2.调用axios对象的方法来发起异步请求

    3.调用axios对象的方法来处理响应的数据

    方法名作用
    get(请求的资源路径与请求的参数)发起GET请求
    post(请求的资源路径,请求的参数)发起POST方式请求
    then(response)请求成功后的回调函数,通过response获取响应的数据
    catch(error)请求失败后的回调函数,通过error获取错误信息
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>异步操作title>
    6. <script src="js/vue.js">script>
    7. <script src="js/axios.js">script>
    8. head>
    9. <body>
    10. <div id="div">
    11. {{name}}
    12. <button @click="send()">发起请求button>
    13. div>
    14. body>
    15. <script>
    16. new Vue({
    17. el:"#div",
    18. data:{
    19. name: "张三"
    20. },
    21. methods:{
    22. send(){
    23. //GET方式请求
    24. // axios.get("testServlet?name=" + this.name)
    25. // .then(resp => {
    26. // alert(resp.data);
    27. // })
    28. // .catch(error => {
    29. // alert(error);
    30. // })
    31. // POST方式请求
    32. axios.post("testServlet","name="+this.name)
    33. .then(resp => {
    34. alert(resp.data);
    35. })
    36. .catch(error => {
    37. alert(error);
    38. })
    39. }
    40. }
    41. });
    42. script>
    43. html>
    1. import javax.servlet.ServletException;
    2. import javax.servlet.annotation.WebServlet;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.IOException;
    7. @WebServlet("/testServlet")
    8. public class TestServlet extends HttpServlet {
    9. @Override
    10. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    11. //设置请求和响应的编码
    12. req.setCharacterEncoding("UTF-8");
    13. resp.setContentType("text/html;charset=UTF-8");
    14. //获取请求参数
    15. String name = req.getParameter("name");
    16. System.out.println(name);
    17. //响应客户端
    18. resp.getWriter().write("请求成功");
    19. }
    20. @Override
    21. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    22. this.doGet(req,resp);
    23. }
    24. }

  • 相关阅读:
    (10)学习笔记:动手深度学习(模型选择 + 过拟合和欠拟合)
    对已经关闭的 channel 进行读写关闭操作会发生什么?
    nginx版本平滑升级(超详细)
    P1387 最大正方形
    阿里云CentOS环境之docker安装,启动,加速器,docker-compose(十四)
    [附源码]JAVA毕业设计个人饮食营养管理信息系统(系统+LW)
    01 MongoDB的概述、应用场景、下载方式、连接方式和发展历史等
    一文带你读懂:TCP连接的三次握手和四次挥手(下篇)
    深拷贝与浅拷贝
    【react】基础知识补充及原理【粗略版】
  • 原文地址:https://blog.csdn.net/weixin_61611746/article/details/133933066