当前位置:   article > 正文

【SpringBoot3+Vue3】四【基础篇】-前端(vue基础)_springboot html vue3

springboot html vue3

目录

一、项目前置知识

二、使用vscode创建

三、vue介绍

四、局部使用vue

1、快速入门

1.1 需求

1.2 准备工作

 1.3 操作

1.3.1 创建html

1.3.2 创建初始html代码

1.3.3 参照官网import  vue

1.3.4 创建vue应用实例

1.3.5 准备div

1.3.6 准备用户数据 

1.3.7 通过插值表达式渲染页面

1.3.8 保存(ctrl+s)运行

2、常用指令

2.1 v-for 遍历

 2.2 v-bind动态为html标签绑定属性值

2.3 v-if & v-show 用来控制元素的显示与隐藏

2.4 v-on为html标签绑定事件

2.5 v-model双向数据绑定

3、生命周期

4、Axios

4.1 介绍

4.2 使用步骤

4.3 别名方式

4.4 实践

4.4.1 导入后端

4.4.2 前端

5、综合案例

五、整站使用vue(工程化)

1、环境准备

2、VUE项目创建和启动

2.1 创建vue3项目

2.2 使用vscode 打开vue项目

2.3 vue项目目录结构

2.4 vue项目启动

3、vue项目开发流程

4、API风格

4.1 组合式API

5、案例

 5.1 启动后台程序

5.2 安装axios

5.3 App.vue代码

 5.4 Article.vue代码

5.5 代码优化1

5.5.1 新增api文件夹

5.5.2 新增article.js文件

5.5.3 修改Article.vue代码

5.6 代码优化2(拦截器使用)

5.6.1 新建文件夹util

 5.6.2 新建request.js文件

5.6.3 优化article.js

六、Element-plus

1、快速入门

1.1 步骤

1.2 实操

 1.2.1 创建vue3项目

1.2.2 进入项目目录安装vue依赖

1.2.3 使用vs studio打开项目

1.2.4 安装Element Plus

1.2.5 项目引用Element Plus

1.2.6 src下新建Button.vue文件

1.2.7 修改 App.vue

2、常用组件

2.1 表格组件

2.1.1 src新增Article.vue文件

2.1.2 修改App.vue

2.2 分页组件 

2.2.1 main.js修改

2.2.2 Article.vue文件导入分页组件

2.3 表单组件

2.3.1 Article.vue文件导入表单组件

2.4 卡片组件

2.4.1 Article.vue文件导入卡片组件


前言:学习vue基础知识,为项目实战铺垫

一、项目前置知识

二、使用vscode创建

创建目录spring3boot3,在文件目录里cmd,输入

  1. # 输入
  2. code .

打开vscode

三、vue介绍

四、局部使用vue

1、快速入门

1.1 需求

1.2 准备工作

 1.3 操作

1.3.1 创建html

1.3.2 创建初始html代码

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. </body>
  10. </html>
1.3.3 参照官网import  vue

快速上手 | Vue.js

 

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <!-- 引入vue模块-->
  10. <script type="module">
  11. import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
  12. </script>
  13. </body>
  14. </html>
1.3.4 创建vue应用实例

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <!-- 引入vue模块-->
  10. <script type="module">
  11. import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
  12. /* 创建vue应用实例 */
  13. createApp({
  14. })
  15. </script>
  16. </body>
  17. </html>
1.3.5 准备div
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. </div>
  11. <!-- 引入vue模块-->
  12. <script type="module">
  13. import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
  14. /* 创建vue应用实例 */
  15. createApp({
  16. }).mount("#app");
  17. </script>
  18. </body>
  19. </html>
1.3.6 准备用户数据 
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. </div>
  11. <!-- 引入vue模块-->
  12. <script type="module">
  13. import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
  14. /* 创建vue应用实例 */
  15. createApp({
  16. data(){
  17. return{
  18. //定义数据
  19. msg: 'hello bocai'
  20. }
  21. }
  22. }).mount("#app");
  23. </script>
  24. </body>
  25. </html>
1.3.7 通过插值表达式渲染页面
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <h1>{{msg}}</h1>
  11. </div>
  12. <!-- 引入vue模块-->
  13. <script type="module">
  14. import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
  15. /* 创建vue应用实例 */
  16. createApp({
  17. data(){
  18. return{
  19. //定义数据
  20. msg:'hello bocai'
  21. }
  22. }
  23. }).mount("#app");
  24. </script>
  25. </body>
  26. </html>
1.3.8 保存(ctrl+s)运行

保存 保存 保存

2、常用指令

2.1 v-for 遍历

语法:

02指令v-for.html 

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <table border="1 solid" colspa="0" cellspacing="0">
  11. <tr>
  12. <th>文章标题</th>
  13. <th>分类</th>
  14. <th>发表时间</th>
  15. <th>状态</th>
  16. <th>操作</th>
  17. </tr>
  18. <!-- 哪个元素要出现多次,v-for指令就添加到哪个元素上 -->
  19. <tr v-for="(article,index) in articleList">
  20. <td>{{article.title}}</td>
  21. <td>{{article.category}}</td>
  22. <td>{{article.time}}</td>
  23. <td>{{article.state}}</td>
  24. <td>
  25. <button>编辑</button>
  26. <button>删除</button>
  27. </td>
  28. </tr>
  29. <!-- <tr>
  30. <td>标题2</td>
  31. <td>分类2</td>
  32. <td>2000-01-01</td>
  33. <td>已发布</td>
  34. <td>
  35. <button>编辑</button>
  36. <button>删除</button>
  37. </td>
  38. </tr>
  39. <tr>
  40. <td>标题3</td>
  41. <td>分类3</td>
  42. <td>2000-01-01</td>
  43. <td>已发布</td>
  44. <td>
  45. <button>编辑</button>
  46. <button>删除</button>
  47. </td>
  48. </tr> -->
  49. </table>
  50. </div>
  51. <script type="module">
  52. //导入vue模块
  53. import { createApp} from
  54. 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  55. //创建应用实例
  56. createApp({
  57. data() {
  58. return {
  59. //定义数据
  60. articleList:[{
  61. title:"医疗反腐绝非砍医护收入",
  62. category:"时事",
  63. time:"2023-09-5",
  64. state:"已发布"
  65. },
  66. {
  67. title:"中国男篮缘何一败涂地?",
  68. category:"篮球",
  69. time:"2023-09-5",
  70. state:"草稿"
  71. },
  72. {
  73. title:"华山景区已受大风影响阵风达7-8级,未来24小时将持续",
  74. category:"旅游",
  75. time:"2023-09-5",
  76. state:"已发布"
  77. }]
  78. }
  79. }
  80. }).mount("#app")//控制页面元素
  81. </script>
  82. </body>
  83. </html>

 

 2.2 v-bind动态为html标签绑定属性值

语法:

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <!-- <a v-bind:href="url">百度一下</a> -->
  11. <a :href="url">百度一下</a>
  12. </div>
  13. <script type="module">
  14. //引入vue模块
  15. import { createApp} from
  16. 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  17. //创建vue应用实例
  18. createApp({
  19. data() {
  20. return {
  21. url: 'https://www.baidu.com'
  22. }
  23. }
  24. }).mount("#app")//控制html元素
  25. </script>
  26. </body>
  27. </html>

2.3 v-if & v-show 用来控制元素的显示与隐藏

语法:

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. 手链价格为: <span v-if="customer.level>=0 && customer.level<=1">9.9</span>
  11. <span v-else-if="customer.level>=2 && customer.level<=4">19.9</span>
  12. <span v-else>29.9</span>
  13. <br/>
  14. 手链价格为: <span v-show="customer.level>=0 && customer.level<=1">9.9</span>
  15. <span v-show="customer.level>=2 && customer.level<=4">19.9</span>
  16. <span v-show="customer.level>=5">29.9</span>
  17. </div>
  18. <script type="module">
  19. //导入vue模块
  20. import { createApp} from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  21. //创建vue应用实例
  22. createApp({
  23. data() {
  24. return {
  25. customer:{
  26. name:'张三',
  27. level:2
  28. }
  29. }
  30. }
  31. }).mount("#app")//控制html元素
  32. </script>
  33. </body>
  34. </html>

2.4 v-on为html标签绑定事件

语法:

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <button v-on:click="money">点我有惊喜</button> &nbsp;
  11. <button @click="love">再点更惊喜</button>
  12. </div>
  13. <script type="module">
  14. //导入vue模块
  15. import { createApp} from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  16. //创建vue应用实例
  17. createApp({
  18. data() {
  19. return {
  20. //定义数据
  21. }
  22. },
  23. methods:{
  24. money: function(){
  25. alert('送你钱100')
  26. },
  27. love: function(){
  28. alert('爱你一万年')
  29. }
  30. }
  31. }).mount("#app");//控制html元素
  32. </script>
  33. </body>
  34. </html>

2.5 v-model双向数据绑定

语法:

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. 文章分类: <input type="text" v-model="searchConditions.category"/> <span>{{searchConditions.category}}</span>
  11. 发布状态: <input type="text" v-model="searchConditions.state"/> <span>{{searchConditions.state}}</span>
  12. <button>搜索</button>
  13. <button v-on:click="clear">重置</button>
  14. <br />
  15. <br />
  16. <table border="1 solid" colspa="0" cellspacing="0">
  17. <tr>
  18. <th>文章标题</th>
  19. <th>分类</th>
  20. <th>发表时间</th>
  21. <th>状态</th>
  22. <th>操作</th>
  23. </tr>
  24. <tr v-for="(article,index) in articleList">
  25. <td>{{article.title}}</td>
  26. <td>{{article.category}}</td>
  27. <td>{{article.time}}</td>
  28. <td>{{article.state}}</td>
  29. <td>
  30. <button>编辑</button>
  31. <button>删除</button>
  32. </td>
  33. </tr>
  34. </table>
  35. </div>
  36. <script type="module">
  37. //导入vue模块
  38. import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  39. //创建vue应用实例
  40. createApp({
  41. data() {
  42. return {
  43. //定义数据
  44. searchConditions:{
  45. category:'',
  46. state:''
  47. },
  48. articleList: [{
  49. title: "医疗反腐绝非砍医护收入",
  50. category: "时事",
  51. time: "2023-09-5",
  52. state: "已发布"
  53. },
  54. {
  55. title: "中国男篮缘何一败涂地?",
  56. category: "篮球",
  57. time: "2023-09-5",
  58. state: "草稿"
  59. },
  60. {
  61. title: "华山景区已受大风影响阵风达7-8级,未来24小时将持续",
  62. category: "旅游",
  63. time: "2023-09-5",
  64. state: "已发布"
  65. }]
  66. }
  67. }
  68. ,
  69. methods:{
  70. clear:function(){
  71. //清空category以及state的数据
  72. //在methods对应的方法里面,使用this就代表的是vue实例,可以使用this获取到vue实例中准备的数据
  73. this.searchConditions.category='';
  74. this.searchConditions.state='';
  75. }
  76. }
  77. }).mount("#app")//控制html元素
  78. </script>
  79. </body>
  80. </html>

3、生命周期

mounted 最频繁 

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. 文章分类: <input type="text" v-model="searchConditions.category"/> <span>{{searchConditions.category}}</span>
  11. 发布状态: <input type="text" v-model="searchConditions.state"/> <span>{{searchConditions.state}}</span>
  12. <button>搜索</button>
  13. <button v-on:click="clear">重置</button>
  14. <br />
  15. <br />
  16. <table border="1 solid" colspa="0" cellspacing="0">
  17. <tr>
  18. <th>文章标题</th>
  19. <th>分类</th>
  20. <th>发表时间</th>
  21. <th>状态</th>
  22. <th>操作</th>
  23. </tr>
  24. <tr v-for="(article,index) in articleList">
  25. <td>{{article.title}}</td>
  26. <td>{{article.category}}</td>
  27. <td>{{article.time}}</td>
  28. <td>{{article.state}}</td>
  29. <td>
  30. <button>编辑</button>
  31. <button>删除</button>
  32. </td>
  33. </tr>
  34. </table>
  35. </div>
  36. <script type="module">
  37. //导入vue模块
  38. import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  39. //创建vue应用实例
  40. createApp({
  41. data() {
  42. return {
  43. //定义数据
  44. searchConditions:{
  45. category:'',
  46. state:''
  47. },
  48. articleList: [{
  49. title: "医疗反腐绝非砍医护收入",
  50. category: "时事",
  51. time: "2023-09-5",
  52. state: "已发布"
  53. },
  54. {
  55. title: "中国男篮缘何一败涂地?",
  56. category: "篮球",
  57. time: "2023-09-5",
  58. state: "草稿"
  59. },
  60. {
  61. title: "华山景区已受大风影响阵风达7-8级,未来24小时将持续",
  62. category: "旅游",
  63. time: "2023-09-5",
  64. state: "已发布"
  65. }]
  66. }
  67. }
  68. ,
  69. methods:{
  70. clear:function(){
  71. //清空category以及state的数据
  72. //在methods对应的方法里面,使用this就代表的是vue实例,可以使用this获取到vue实例中准备的数据
  73. this.searchConditions.category='';
  74. this.searchConditions.state='';
  75. }
  76. }
  77. ,
  78. mounted:function(){
  79. console.log('Vue挂载完毕,发送请求获取数据')
  80. }
  81. }).mount("#app")//控制html元素
  82. </script>
  83. </body>
  84. </html>

4、Axios

4.1 介绍

 

4.2 使用步骤

4.3 别名方式

4.4 实践

4.4.1 导入后端

前面课程的存在token,不好处理

后端代码:

链接:https://pan.baidu.com/s/1H2kd77Brg8zblMj7fnMuoQ 
提取码:关注联系我,私信我,发本文链接

我下载下来pom做了一些改变,主要是springboot版本等与前面后端的课程保存一致性

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.1.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.itheima</groupId>
  12. <artifactId>axios_demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>axios_demo</name>
  15. <description>axios_demo</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.projectlombok</groupId>
  31. <artifactId>lombok</artifactId>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-maven-plugin</artifactId>
  39. </plugin>
  40. </plugins>
  41. </build>
  42. </project>

 导入的时候卡主,注意自己的maven配置

不知道跟.idea里面这个是否有关 workspace.xml,导致卡主。修改这个maven估计就好了

4.4.2 前端

默认方式与别名方式

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <!-- 引入axios的js文件 -->
  10. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  11. <script>
  12. /* 发送请求 */
  13. /* axios({
  14. method:'get',
  15. url:'http://localhost:8080/article/getAll'
  16. }).then(result=>{
  17. //成功的回调
  18. //result代表服务器响应的所有的数据,包含了响应头,响应体. result.data 代表的是接口响应的核心数据
  19. console.log(result.data);
  20. }).catch(err=>{
  21. //失败的回调
  22. console.log(err);
  23. }); */
  24. let article = {
  25. title: '明天会更好',
  26. category: '生活',
  27. time: '2000-01-01',
  28. state: '草稿'
  29. }
  30. /* axios({
  31. method:'post',
  32. url:'http://localhost:8080/article/add',
  33. data:article
  34. }).then(result=>{
  35. //成功的回调
  36. //result代表服务器响应的所有的数据,包含了响应头,响应体. result.data 代表的是接口响应的核心数据
  37. console.log(result.data);
  38. }).catch(err=>{
  39. //失败的回调
  40. console.log(err);
  41. }); */
  42. //别名的方式发送请求
  43. /* axios.get('http://localhost:8080/article/getAll').then(result => {
  44. //成功的回调
  45. //result代表服务器响应的所有的数据,包含了响应头,响应体. result.data 代表的是接口响应的核心数据
  46. console.log(result.data);
  47. }).catch(err => {
  48. //失败的回调
  49. console.log(err);
  50. }); */
  51. axios.post('http://localhost:8080/article/add', article).then(result => {
  52. //成功的回调
  53. //result代表服务器响应的所有的数据,包含了响应头,响应体. result.data 代表的是接口响应的核心数据
  54. console.log(result.data);
  55. }).catch(err => {
  56. //失败的回调
  57. console.log(err);
  58. });
  59. </script>
  60. </body>
  61. </html>

5、综合案例

  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. 文章分类: <input type="text" v-model="searchConditions.category">
  11. 发布状态: <input type="text" v-model="searchConditions.state">
  12. <button v-on:click="search">搜索</button>
  13. <button v-on:click="clear">重置</button>
  14. <br />
  15. <br />
  16. <table border="1 solid" colspa="0" cellspacing="0">
  17. <tr>
  18. <th>文章标题</th>
  19. <th>分类</th>
  20. <th>发表时间</th>
  21. <th>状态</th>
  22. <th>操作</th>
  23. </tr>
  24. <tr v-for="(article,index) in articleList">
  25. <td>{{article.title}}</td>
  26. <td>{{article.category}}</td>
  27. <td>{{article.time}}</td>
  28. <td>{{article.state}}</td>
  29. <td>
  30. <button>编辑</button>
  31. <button>删除</button>
  32. </td>
  33. </tr>
  34. <!-- <tr>
  35. <td>标题2</td>
  36. <td>分类2</td>
  37. <td>2000-01-01</td>
  38. <td>已发布</td>
  39. <td>
  40. <button>编辑</button>
  41. <button>删除</button>
  42. </td>
  43. </tr>
  44. <tr>
  45. <td>标题3</td>
  46. <td>分类3</td>
  47. <td>2000-01-01</td>
  48. <td>已发布</td>
  49. <td> -->
  50. <button>编辑</button>
  51. <button>删除</button>
  52. </td>
  53. </tr>
  54. </table>
  55. </div>
  56. <!-- 引入axios的js文件 -->
  57. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  58. <script type="module">
  59. //导入vue模块
  60. import {createApp} from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  61. // 创建vue应用实例
  62. createApp({
  63. data(){
  64. return{
  65. articleList:[],
  66. searchConditions:{
  67. category:'',
  68. state:''
  69. }
  70. }
  71. },
  72. methods:{
  73. //声明方法
  74. search:function(){
  75. //发送请求,完成搜索,携带搜索条件
  76. axios.get('http://localhost:8080/article/search?category='+this.searchConditions.category+'&state='+this.searchConditions.state)
  77. .then(result=>{
  78. //成功回调 result.data
  79. //把得到的数据赋值给articleList
  80. this.articleList=result.data
  81. }).catch(err=>{
  82. console.log(err);
  83. });
  84. },
  85. clear:function(){
  86. //清空category以及state的数据
  87. //在methods对应的方法里面,使用this就代表的是vue实例,可以使用this获取到vue实例中准备的数据
  88. this.searchConditions.category='';
  89. this.searchConditions.state='';
  90. }
  91. },
  92. mounted:function(){
  93. //发送异步请求 axios
  94. axios.get('http://localhost:8080/article/getAll').then(result =>{
  95. //成功回调
  96. // console.log(result.data);
  97. this.articleList = result.data
  98. }).catch(err =>{
  99. //失败回调
  100. console.log(err);
  101. })
  102. }
  103. }).mount('#app'); // 控制html元素
  104. </script>
  105. </body>
  106. </html>

五、整站使用vue(工程化)

1、环境准备

看这个也行《nodejs》

2、VUE项目创建和启动

2.1 创建vue3项目

npm init vue@latest

 

 实际操作

npm init vue@latest

# 输入项目名称,然后全部默认

  1. # 进到项目目录
  2. cd vue3-project

  1. # 输入命令 安装项目vue依赖
  2. npm install

2.2 使用vscode 打开vue项目

在项目目录输入命令code .

code .

 

2.3 vue项目目录结构

2.4 vue项目启动

下图有两种方式启动 1与2

 

3、vue项目开发流程

 

4、API风格

4.1 组合式API

 App.vue

  1. <!-- <script>
  2. //写数据
  3. export default{
  4. data(){
  5. return{
  6. msg: '上海'
  7. }
  8. }
  9. }
  10. </script> -->
  11. <script setup>
  12. import {ref} from 'vue';
  13. //调用ref函数,定义响应式数据
  14. const msg = ref('西安');
  15. //导入Api.vue文件 vs 开发工具显示下面一句有问题 是开发工具问题
  16. import ApiVue from './Api.vue'
  17. </script>
  18. <template>
  19. <!-- html-->
  20. <h1>{{ msg }}</h1>
  21. <ApiVue/>
  22. </template>
  23. <style scoped>
  24. /* 样式 */
  25. h1{
  26. color: red;
  27. }
  28. </style>

Api.vue

  1. <script setup>
  2. import {onMounted, ref} from 'vue';
  3. //声明响应式数据 ref 响应式对象有一个内部的属性value
  4. const count = ref(0);
  5. // 声明函数increment
  6. function increment(){
  7. count.value++;
  8. }
  9. //声明钩子函数onMounted
  10. onMounted(()=>{
  11. console.log('vue已经挂载完毕!')
  12. });
  13. </script>
  14. <template>
  15. <button @click="increment">点击+1 count:{{ count }}</button>
  16. </template>

5、案例

 5.1 启动后台程序

后台程序与局部的那个案例相同

5.2 安装axios

项目目录下执行

npm install axios

5.3 App.vue代码

  1. <!-- <script>
  2. //写数据
  3. export default{
  4. data(){
  5. return{
  6. msg: '上海'
  7. }
  8. }
  9. }
  10. </script> -->
  11. <script setup>
  12. import {ref} from 'vue';
  13. //调用ref函数,定义响应式数据
  14. const msg = ref('西安');
  15. //导入Api.vue文件 vs 开发工具显示下面一句有问题 是开发工具问题
  16. import ApiVue from './Api.vue'
  17. //Article.vue文件 vs 开发工具显示下面一句有问题 是开发工具问题
  18. import Article from './Article.vue'
  19. </script>
  20. <template>
  21. <!-- html-->
  22. <!-- <h1>{{ msg }}</h1>
  23. <br>
  24. <ApiVue/> -->
  25. <Article/>
  26. </template>
  27. <style scoped>
  28. /* 样式 */
  29. h1{
  30. color: red;
  31. }
  32. </style>

 5.4 Article.vue代码

  1. <script setup>
  2. //导入axios
  3. import axios from 'axios';
  4. import {ref} from 'vue';
  5. //定义响应式数据 ref
  6. const articleList = ref([])
  7. //发送异步请求,获取所有文章数据
  8. axios.get('http://localhost:8080/article/getAll')
  9. .then(result=>{
  10. //把服务器相应的数据保存起来
  11. articleList.value = result.data;
  12. }).catch(err=>{
  13. console.log(err)
  14. });
  15. //定义响应式数据searchConditions
  16. const searchConditions = ref({
  17. category:'',
  18. state:''
  19. });
  20. //声明search函数
  21. const search=function(){
  22. //发送请求完成搜索
  23. axios.get('http://localhost:8080/article/search',{params:{...searchConditions.value}})
  24. .then(result=>{
  25. articleList.value = result.data;
  26. }).catch(err=>{
  27. console.log(err)
  28. });
  29. }
  30. </script>
  31. <template>
  32. <!-- html元素-->
  33. <div>
  34. 文章分类: <input type="text" v-model="searchConditions.category">
  35. 发布状态: <input type="text" v-model="searchConditions.state">
  36. <button v-on:click="search">搜索</button>
  37. <br />
  38. <br />
  39. <table border="1 solid" colspa="0" cellspacing="0">
  40. <tr>
  41. <th>文章标题</th>
  42. <th>分类</th>
  43. <th>发表时间</th>
  44. <th>状态</th>
  45. <th>操作</th>
  46. </tr>
  47. <tr v-for="(article,index) in articleList">
  48. <td>{{article.title}}</td>
  49. <td>{{article.category}}</td>
  50. <td>{{article.time}}</td>
  51. <td>{{article.state}}</td>
  52. <td>
  53. <button>编辑</button>
  54. <button>删除</button>
  55. </td>
  56. </tr>
  57. <!-- <tr>
  58. <td>标题2</td>
  59. <td>分类2</td>
  60. <td>2000-01-01</td>
  61. <td>已发布</td>
  62. <td>
  63. <button>编辑</button>
  64. <button>删除</button>
  65. </td>
  66. </tr>
  67. <tr>
  68. <td>标题3</td>
  69. <td>分类3</td>
  70. <td>2000-01-01</td>
  71. <td>已发布</td>
  72. <td>
  73. <button>编辑</button>
  74. <button>删除</button>
  75. </td>
  76. </tr> -->
  77. </table>
  78. </div>
  79. </template>

5.5 代码优化1

问题:

5.5.1 新增api文件夹

5.5.2 新增article.js文件

  1. //导入axios
  2. import axios from 'axios';
  3. //定义一个变量,记录公共的前缀baseURL
  4. const baseURL = 'http://localhost:8080'
  5. const instance = axios.create({baseURL})
  6. //获取所有文件的数据函数
  7. export async function articleGetAllService(){
  8. //发送异步请求,获取所有文章数据
  9. //同步等待服务器相应的结果,并返回async,await
  10. // return await axios.get('/article/getAll')
  11. return await instance.get('/article/getAll')
  12. .then(result=>{
  13. //把服务器相应的数据保存起来
  14. // articleList.value = result.data;
  15. return result.data;
  16. }).catch(err=>{
  17. console.log(err)
  18. });
  19. }
  20. //根据文章分类和发包状态搜索的函数
  21. export async function articleGetSearchService(conditions){
  22. //发送请求完成搜索
  23. // return await axios.get('/article/search',{params : conditions})
  24. return await instance.get('/article/search',{params : conditions})
  25. .then(result=>{
  26. // articleList.value = result.data;
  27. return result.data;
  28. }).catch(err=>{
  29. console.log(err)
  30. });
  31. }
5.5.3 修改Article.vue代码
  1. <script setup>
  2. import {articleGetAllService,articleGetSearchService} from '@/api/article.js'
  3. import {ref} from 'vue';
  4. //定义响应式数据 ref
  5. const articleList = ref([])
  6. //获取所有文章数据
  7. //同步获取articleGetAllService的返回结果 async await
  8. const getAllArticle = async function(){
  9. let data = await articleGetAllService()
  10. articleList.value = data;
  11. }
  12. getAllArticle();
  13. //定义响应式数据searchConditions
  14. const searchConditions = ref({
  15. category:'',
  16. state:''
  17. });
  18. //声明search函数
  19. const search= async function(){
  20. //文章搜索
  21. let data = await articleGetSearchService({...searchConditions.value});
  22. articleList.value = data;
  23. }
  24. </script>
  25. <template>
  26. <!-- html元素-->
  27. <div>
  28. 文章分类: <input type="text" v-model="searchConditions.category">
  29. 发布状态: <input type="text" v-model="searchConditions.state">
  30. <button v-on:click="search">搜索</button>
  31. <br />
  32. <br />
  33. <table border="1 solid" colspa="0" cellspacing="0">
  34. <tr>
  35. <th>文章标题</th>
  36. <th>分类</th>
  37. <th>发表时间</th>
  38. <th>状态</th>
  39. <th>操作</th>
  40. </tr>
  41. <tr v-for="(article,index) in articleList">
  42. <td>{{article.title}}</td>
  43. <td>{{article.category}}</td>
  44. <td>{{article.time}}</td>
  45. <td>{{article.state}}</td>
  46. <td>
  47. <button>编辑</button>
  48. <button>删除</button>
  49. </td>
  50. </tr>
  51. <!-- <tr>
  52. <td>标题2</td>
  53. <td>分类2</td>
  54. <td>2000-01-01</td>
  55. <td>已发布</td>
  56. <td>
  57. <button>编辑</button>
  58. <button>删除</button>
  59. </td>
  60. </tr>
  61. <tr>
  62. <td>标题3</td>
  63. <td>分类3</td>
  64. <td>2000-01-01</td>
  65. <td>已发布</td>
  66. <td>
  67. <button>编辑</button>
  68. <button>删除</button>
  69. </td>
  70. </tr> -->
  71. </table>
  72. </div>
  73. </template>

5.6 代码优化2(拦截器使用)

问题

 

5.6.1 新建文件夹util

 5.6.2 新建request.js文件
  1. // 定制请求实例
  2. //导入axios
  3. import axios from 'axios';
  4. //定义一个变量,记录公共的前缀baseURL
  5. const baseURL = 'http://localhost:8080'
  6. const instance = axios.create({baseURL})
  7. // 添加响应拦截器
  8. instance.interceptors.response.use(
  9. result=>{
  10. return result.data;
  11. },
  12. err=>{
  13. alert('服务异常')
  14. return Promise.reject(err);//异步的状态转化成失败的状态
  15. }
  16. )
  17. export default instance;
5.6.3 优化article.js
  1. /*
  2. //导入axios
  3. import axios from 'axios';
  4. //定义一个变量,记录公共的前缀baseURL
  5. const baseURL = 'http://localhost:8080'
  6. const instance = axios.create({baseURL}) */
  7. import request from '@/util/request.js'
  8. //获取所有文件的数据函数
  9. export async function articleGetAllService(){
  10. //发送异步请求,获取所有文章数据
  11. //同步等待服务器相应的结果,并返回async,await
  12. // return await axios.get('/article/getAll')
  13. return request.get('/article/getAll');
  14. }
  15. //根据文章分类和发包状态搜索的函数
  16. export async function articleGetSearchService(conditions){
  17. //发送请求完成搜索
  18. // return await axios.get('/article/search',{params : conditions})
  19. return request.get('/article/search',{params : conditions});
  20. }

六、Element-plus

1、快速入门

1.1 步骤

1.2 实操

 1.2.1 创建vue3项目
npm init vue@latest

 

1.2.2 进入项目目录安装vue依赖
  1. cd 项目目录
  2. npm install

1.2.3 使用vs studio打开项目
code .

1.2.4 安装Element Plus
npm install element-plus --save

1.2.5 项目引用Element Plus

复制替换main.js文件内容

  1. // main.ts
  2. import { createApp } from 'vue' //导入vue
  3. import ElementPlus from 'element-plus' //导入element-plus
  4. import 'element-plus/dist/index.css' //导入element-plus的样式
  5. import App from './App.vue' //导入app.vue
  6. const app = createApp(App) //创建应用实例
  7. app.use(ElementPlus) //使用element-plus
  8. app.mount('#app') //控制html元素
1.2.6 src下新建Button.vue文件
  1. <script lang="ts" setup>
  2. import {
  3. Check,
  4. Delete,
  5. Edit,
  6. Message,
  7. Search,
  8. Star,
  9. } from '@element-plus/icons-vue'
  10. </script>
  11. <template>
  12. <el-row class="mb-4">
  13. <el-button>Default</el-button>
  14. <el-button type="primary">Primary</el-button>
  15. <el-button type="success">Success</el-button>
  16. <el-button type="info">Info</el-button>
  17. <el-button type="warning">Warning</el-button>
  18. <el-button type="danger">Danger</el-button>
  19. </el-row>
  20. <el-row class="mb-4">
  21. <el-button plain>Plain</el-button>
  22. <el-button type="primary" plain>Primary</el-button>
  23. <el-button type="success" plain>Success</el-button>
  24. <el-button type="info" plain>Info</el-button>
  25. <el-button type="warning" plain>Warning</el-button>
  26. <el-button type="danger" plain>Danger</el-button>
  27. </el-row>
  28. <el-row class="mb-4">
  29. <el-button round>Round</el-button>
  30. <el-button type="primary" round>Primary</el-button>
  31. <el-button type="success" round>Success</el-button>
  32. <el-button type="info" round>Info</el-button>
  33. <el-button type="warning" round>Warning</el-button>
  34. <el-button type="danger" round>Danger</el-button>
  35. </el-row>
  36. <el-row>
  37. <el-button :icon="Search" circle />
  38. <el-button type="primary" :icon="Edit" circle />
  39. <el-button type="success" :icon="Check" circle />
  40. <el-button type="info" :icon="Message" circle />
  41. <el-button type="warning" :icon="Star" circle />
  42. <el-button type="danger" :icon="Delete" circle />
  43. </el-row>
  44. </template>
1.2.7 修改 App.vue
  1. <script setup>
  2. import ButtonVue from './Button.vue';
  3. </script>
  4. <template>
  5. <ButtonVue/>
  6. </template>
  1. # 启动项目
  2. npm run dev

 

优化代码

  1. <script lang="ts" setup>
  2. import {
  3. Check,
  4. Delete,
  5. Edit,
  6. Message,
  7. Search,
  8. Star,
  9. } from '@element-plus/icons-vue'
  10. </script>
  11. <template>
  12. <el-row class="mb-4">
  13. <el-button>Default</el-button>
  14. <el-button type="primary" disabled = "true">编辑</el-button> <!--disabled = "true" 禁用-->
  15. <el-button type="success" loading = "true">查看</el-button> <!--loading = "true" 加载中-->
  16. </el-row>
  17. <el-row class="mb-4">
  18. <el-button type="info" plain>Info</el-button>
  19. <el-button type="warning" plain>Warning</el-button>
  20. <el-button type="danger" plain>Danger</el-button>
  21. </el-row>
  22. <!-- 初始化的
  23. <el-row class="mb-4">
  24. <el-button>Default</el-button>
  25. <el-button type="primary">Primary</el-button>
  26. <el-button type="success">Success</el-button>
  27. <el-button type="info">Info</el-button>
  28. <el-button type="warning">Warning</el-button>
  29. <el-button type="danger">Danger</el-button>
  30. </el-row>
  31. <el-row class="mb-4">
  32. <el-button plain>Plain</el-button>
  33. <el-button type="primary" plain>Primary</el-button>
  34. <el-button type="success" plain>Success</el-button>
  35. <el-button type="info" plain>Info</el-button>
  36. <el-button type="warning" plain>Warning</el-button>
  37. <el-button type="danger" plain>Danger</el-button>
  38. </el-row>
  39. <el-row class="mb-4">
  40. <el-button round>Round</el-button>
  41. <el-button type="primary" round>Primary</el-button>
  42. <el-button type="success" round>Success</el-button>
  43. <el-button type="info" round>Info</el-button>
  44. <el-button type="warning" round>Warning</el-button>
  45. <el-button type="danger" round>Danger</el-button>
  46. </el-row>
  47. <el-row>
  48. <el-button :icon="Search" circle />
  49. <el-button type="primary" :icon="Edit" circle />
  50. <el-button type="success" :icon="Check" circle />
  51. <el-button type="info" :icon="Message" circle />
  52. <el-button type="warning" :icon="Star" circle />
  53. <el-button type="danger" :icon="Delete" circle />
  54. </el-row> -->
  55. </template>

2、常用组件

2.1 表格组件

2.1.1 src新增Article.vue文件
  1. <script lang="ts" setup>
  2. import {
  3. Delete,
  4. Edit,
  5. } from '@element-plus/icons-vue'
  6. const tableData = [
  7. {
  8. title: '标题1',
  9. category: '时事',
  10. time: '2023-01-02',
  11. state: '已发布',
  12. },
  13. {
  14. title: '标题1',
  15. category: '时事',
  16. time: '2023-01-02',
  17. state: '已发布',
  18. },
  19. {
  20. title: '标题1',
  21. category: '时事',
  22. time: '2023-01-02',
  23. state: '已发布',
  24. },
  25. {
  26. title: '标题1',
  27. category: '时事',
  28. time: '2023-01-02',
  29. state: '已发布',
  30. }
  31. ]
  32. </script>
  33. <template>
  34. <el-table :data="tableData" style="width: 100%">
  35. <el-table-column prop="title" label="文章标题"/>
  36. <el-table-column prop="category" label="分类"/>
  37. <el-table-column prop="time" label="发表时间" />
  38. <el-table-column prop="state" label="状态" />
  39. <el-table-column label="操作" width="180" >
  40. <el-row>
  41. <el-button type="primary" :icon="Edit" circle />
  42. <el-button type="danger" :icon="Delete" circle />
  43. </el-row>
  44. </el-table-column>
  45. </el-table>
  46. </template>
2.1.2 修改App.vue
  1. <script setup>
  2. import ButtonVue from './Button.vue';
  3. import ArticleVue from './Article.vue';
  4. </script>
  5. <template>
  6. <!-- <ButtonVue/> -->
  7. <ArticleVue/>
  8. </template>

2.2 分页组件 

2.2.1 main.js修改

支持分页控件中文显示

  1. // main.ts
  2. import { createApp } from 'vue' //导入vue
  3. import ElementPlus from 'element-plus' //导入element-plus
  4. import 'element-plus/dist/index.css' //导入element-plus的样式
  5. import App from './App.vue' //导入app.vue
  6. import locale from 'element-plus/dist/locale/zh-cn.js' //调整分页控件中文显示
  7. const app = createApp(App) //创建应用实例
  8. app.use(ElementPlus,{locale}) //使用element-plus
  9. app.mount('#app') //控制html元素
2.2.2 Article.vue文件导入分页组件
  1. <script lang="ts" setup>
  2. // 按钮icon
  3. import {
  4. Delete,
  5. Edit,
  6. } from '@element-plus/icons-vue'
  7. // 分页
  8. import { ref } from 'vue'
  9. const currentPage4 = ref(4)
  10. const pageSize4 = ref(5)
  11. const small = ref(false)
  12. const background = ref(false)
  13. const disabled = ref(false)
  14. const total = ref(20)
  15. const handleSizeChange = (val: number) => {
  16. console.log(`${val} items per page`)
  17. }
  18. const handleCurrentChange = (val: number) => {
  19. console.log(`current page: ${val}`)
  20. }
  21. const tableData = [
  22. {
  23. title: '标题1',
  24. category: '时事',
  25. time: '2023-01-02',
  26. state: '已发布',
  27. },
  28. {
  29. title: '标题1',
  30. category: '时事',
  31. time: '2023-01-02',
  32. state: '已发布',
  33. },
  34. {
  35. title: '标题1',
  36. category: '时事',
  37. time: '2023-01-02',
  38. state: '已发布',
  39. },
  40. {
  41. title: '标题1',
  42. category: '时事',
  43. time: '2023-01-02',
  44. state: '已发布',
  45. }
  46. ]
  47. </script>
  48. <template>
  49. <!-- 表格组件 -->
  50. <el-table :data="tableData" style="width: 100%">
  51. <el-table-column prop="title" label="文章标题"/>
  52. <el-table-column prop="category" label="分类"/>
  53. <el-table-column prop="time" label="发表时间" />
  54. <el-table-column prop="state" label="状态" />
  55. <el-table-column label="操作" width="180" >
  56. <el-row>
  57. <el-button type="primary" :icon="Edit" circle />
  58. <el-button type="danger" :icon="Delete" circle />
  59. </el-row>
  60. </el-table-column>
  61. </el-table>
  62. <!-- 分页组件 -->
  63. <el-pagination
  64. class="el-p"
  65. v-model:current-page="currentPage4"
  66. v-model:page-size="pageSize4"
  67. :page-sizes="[5, 10, 15, 40200]"
  68. :small="small"
  69. :disabled="disabled"
  70. :background="background"
  71. layout="jumper, total, sizes, prev, pager, next"
  72. :total="total"
  73. @size-change="handleSizeChange"
  74. @current-change="handleCurrentChange"
  75. />
  76. </template>
  77. <style scoped>
  78. .el-p{
  79. margin-top: 20px;
  80. display: flex;
  81. justify-content: flex-end;
  82. }
  83. </style>

2.3 表单组件

这里使用的是行内表单

2.3.1 Article.vue文件导入表单组件
  1. <script lang="ts" setup>
  2. // 按钮icon
  3. import {
  4. Delete,
  5. Edit,
  6. } from '@element-plus/icons-vue'
  7. // 分页组件导入
  8. import { ref } from 'vue'
  9. // 表单组件 行内表单
  10. import { reactive } from 'vue'
  11. const formInline = reactive({
  12. user: '',
  13. region: '',
  14. date: '',
  15. })
  16. const onSubmit = () => {
  17. console.log('submit!')
  18. }
  19. // 分页 组件
  20. const currentPage4 = ref(4)
  21. const pageSize4 = ref(5)
  22. const small = ref(false)
  23. const background = ref(false)
  24. const disabled = ref(false)
  25. const total = ref(20)
  26. const handleSizeChange = (val: number) => {
  27. console.log(`${val} items per page`)
  28. }
  29. const handleCurrentChange = (val: number) => {
  30. console.log(`current page: ${val}`)
  31. }
  32. const tableData = [
  33. {
  34. title: '标题1',
  35. category: '时事',
  36. time: '2023-01-02',
  37. state: '已发布',
  38. },
  39. {
  40. title: '标题1',
  41. category: '时事',
  42. time: '2023-01-02',
  43. state: '已发布',
  44. },
  45. {
  46. title: '标题1',
  47. category: '时事',
  48. time: '2023-01-02',
  49. state: '已发布',
  50. },
  51. {
  52. title: '标题1',
  53. category: '时事',
  54. time: '2023-01-02',
  55. state: '已发布',
  56. }
  57. ]
  58. </script>
  59. <template>
  60. <!-- 表单组件 行内表单 -->
  61. <el-form :inline="true" :model="formInline" class="demo-form-inline">
  62. <!-- 查询条件1 -->
  63. <el-form-item label="文章分类:">
  64. <el-select
  65. v-model="formInline.region"
  66. placeholder="请选择"
  67. clearable
  68. >
  69. <el-option label="时事" value="时事" />
  70. <el-option label="篮球" value="篮球" />
  71. </el-select>
  72. </el-form-item>
  73. <!-- 查询条件2 -->
  74. <el-form-item label="发布状态:">
  75. <el-select
  76. v-model="formInline.region"
  77. placeholder="请选择"
  78. clearable
  79. >
  80. <el-option label="已发布" value="已发布" />
  81. <el-option label="草稿" value="草稿" />
  82. </el-select>
  83. </el-form-item>
  84. <!-- 查询按钮-->
  85. <el-form-item>
  86. <el-button type="primary" @click="onSubmit">查询</el-button>
  87. </el-form-item>
  88. <!-- 重置按钮 -->
  89. <el-form-item>
  90. <el-button type="default" @click="onSubmit">重置</el-button>
  91. </el-form-item>
  92. </el-form>
  93. <!-- 表格组件 -->
  94. <el-table :data="tableData" style="width: 100%">
  95. <el-table-column prop="title" label="文章标题"/>
  96. <el-table-column prop="category" label="分类"/>
  97. <el-table-column prop="time" label="发表时间" />
  98. <el-table-column prop="state" label="状态" />
  99. <el-table-column label="操作" width="180" >
  100. <el-row>
  101. <el-button type="primary" :icon="Edit" circle />
  102. <el-button type="danger" :icon="Delete" circle />
  103. </el-row>
  104. </el-table-column>
  105. </el-table>
  106. <!-- 分页组件 -->
  107. <el-pagination
  108. class="el-p"
  109. v-model:current-page="currentPage4"
  110. v-model:page-size="pageSize4"
  111. :page-sizes="[5, 10, 15, 40200]"
  112. :small="small"
  113. :disabled="disabled"
  114. :background="background"
  115. layout="jumper, total, sizes, prev, pager, next"
  116. :total="total"
  117. @size-change="handleSizeChange"
  118. @current-change="handleCurrentChange"
  119. />
  120. </template>
  121. <style scoped>
  122. .el-p{
  123. margin-top: 20px;
  124. display: flex;
  125. justify-content: flex-end;
  126. }
  127. </style>

2.4 卡片组件

2.4.1 Article.vue文件导入卡片组件

导入卡片组件,将之前的组件放放在卡片组件内

  1. <script lang="ts" setup>
  2. // 按钮icon
  3. import {
  4. Delete,
  5. Edit,
  6. } from '@element-plus/icons-vue'
  7. // 分页组件导入
  8. import { ref } from 'vue'
  9. // 表单组件 行内表单
  10. import { reactive } from 'vue'
  11. const formInline = reactive({
  12. user: '',
  13. region: '',
  14. date: '',
  15. })
  16. const onSubmit = () => {
  17. console.log('submit!')
  18. }
  19. // 分页 组件
  20. const currentPage4 = ref(4)
  21. const pageSize4 = ref(5)
  22. const small = ref(false)
  23. const background = ref(false)
  24. const disabled = ref(false)
  25. const total = ref(20)
  26. const handleSizeChange = (val: number) => {
  27. console.log(`${val} items per page`)
  28. }
  29. const handleCurrentChange = (val: number) => {
  30. console.log(`current page: ${val}`)
  31. }
  32. const tableData = [
  33. {
  34. title: '标题1',
  35. category: '时事',
  36. time: '2023-01-02',
  37. state: '已发布',
  38. },
  39. {
  40. title: '标题1',
  41. category: '时事',
  42. time: '2023-01-02',
  43. state: '已发布',
  44. },
  45. {
  46. title: '标题1',
  47. category: '时事',
  48. time: '2023-01-02',
  49. state: '已发布',
  50. },
  51. {
  52. title: '标题1',
  53. category: '时事',
  54. time: '2023-01-02',
  55. state: '已发布',
  56. }
  57. ]
  58. </script>
  59. <template>
  60. <!-- 卡片组件 -->
  61. <el-card class="box-card">
  62. <div class="card-header">
  63. <span>文章管理</span>
  64. <el-button type="primary">发布文章</el-button>
  65. </div>
  66. <div style="margin-top: 20px;">
  67. <hr>
  68. </div>
  69. <!-- 表单组件 行内表单 -->
  70. <el-form :inline="true" :model="formInline" class="demo-form-inline">
  71. <!-- 查询条件1 -->
  72. <el-form-item label="文章分类:">
  73. <el-select
  74. v-model="formInline.region"
  75. placeholder="请选择"
  76. clearable
  77. >
  78. <el-option label="时事" value="时事" />
  79. <el-option label="篮球" value="篮球" />
  80. </el-select>
  81. </el-form-item>
  82. <!-- 查询条件2 -->
  83. <el-form-item label="发布状态:">
  84. <el-select
  85. v-model="formInline.region"
  86. placeholder="请选择"
  87. clearable
  88. >
  89. <el-option label="已发布" value="已发布" />
  90. <el-option label="草稿" value="草稿" />
  91. </el-select>
  92. </el-form-item>
  93. <!-- 查询按钮-->
  94. <el-form-item>
  95. <el-button type="primary" @click="onSubmit">查询</el-button>
  96. </el-form-item>
  97. <!-- 重置按钮 -->
  98. <el-form-item>
  99. <el-button type="default" @click="onSubmit">重置</el-button>
  100. </el-form-item>
  101. </el-form>
  102. <!-- 表格组件 -->
  103. <el-table :data="tableData" style="width: 100%">
  104. <el-table-column prop="title" label="文章标题"/>
  105. <el-table-column prop="category" label="分类"/>
  106. <el-table-column prop="time" label="发表时间" />
  107. <el-table-column prop="state" label="状态" />
  108. <el-table-column label="操作" width="180" >
  109. <el-row>
  110. <el-button type="primary" :icon="Edit" circle />
  111. <el-button type="danger" :icon="Delete" circle />
  112. </el-row>
  113. </el-table-column>
  114. </el-table>
  115. <!-- 分页组件 -->
  116. <el-pagination
  117. class="el-p"
  118. v-model:current-page="currentPage4"
  119. v-model:page-size="pageSize4"
  120. :page-sizes="[5, 10, 15, 40200]"
  121. :small="small"
  122. :disabled="disabled"
  123. :background="background"
  124. layout="jumper, total, sizes, prev, pager, next"
  125. :total="total"
  126. @size-change="handleSizeChange"
  127. @current-change="handleCurrentChange"
  128. />
  129. </el-card>
  130. </template>
  131. <style scoped>
  132. .el-p{
  133. margin-top: 20px;
  134. display: flex;
  135. justify-content: flex-end;
  136. }
  137. .card-header{
  138. display: flex;
  139. justify-content: space-between;
  140. }
  141. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/567660
推荐阅读
相关标签
  

闽ICP备14008679号