当前位置: 首页 > news >正文

河北省建设网站锁安装什么驱动网站制作效果好

河北省建设网站锁安装什么驱动,网站制作效果好,做网站链接 赚钱 违法吗,招聘网站页面Vue实战2 通过create-vue创建Vue脚手架(npm为NodeJs的软件包管理器) Vue的创建 npm init vuelatest创建项目基本的选项 Vue项目的目录结构 启动Vue项目:npm run dev 或者通过npm脚本中的dev启动(vscode中的npm脚本打开方式通…

Vue实战2


通过create-vue创建Vue脚手架(npm为NodeJs的软件包管理器)

Vue的创建

npm init vue@latest创建项目基本的选项
在这里插入图片描述

Vue项目的目录结构

在这里插入图片描述

启动Vue项目:npm run dev

或者通过npm脚本中的dev启动(vscode中的npm脚本打开方式通过点击package.json文件就会出现)

Vue项目的入口文件是main.js
执行流程:
在这里插入图片描述

通过main.js的createApp方法将对应的App.vue导入

createApp(App).mount('#app')

在通过mount将此Vue实例关联到index.html中

<!DOCTYPE html>
<html lang=""><head><meta charset="UTF-8"><link rel="icon" href="/favicon.ico"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Vite App</title></head><body><div id="app"></div><script type="module" src="/src/main.js"></script></body>
</html>

App.vue分为三个部分

template:编写HTML代码

style:编写CSS代码

script:编写JS代码

<!-- <script>
// 写数据
// 默认导出---》用于填充在main.js中的createApp()方法中
export default {data() {return {msg: "上海"};}
};
</script> -->
<script setup>import { ref } from "vue";const msg = ref("西安");//ref 创建响应式数据
</script>
<template><!-- HTML代码 --><!-- <h1>北京</h1> --><h1>{{ msg }}</h1>
</template><style scoped>
/* CSS代码 */
h1 {color: red;
}
</style>


Vue的API风格(组合式API和选项式API)

在这里插入图片描述

组合式API必须通过.value才能获取或者修改数据

Api.vue:

<script setup>import { ref,onMounted } from 'vue'// 声明响应式数据 ref,响应式的对象内部有一个属性 value// 在组合式API中一般需要将数据定义为响应式数据const count = ref(0);// 声明函数const increment = () => {count.value++;}// 声明钩子函数onMounted(() => {console.log('组件挂载完毕');})
</script><template><!-- 写HTML元素 --><button @click="increment">count:{{ count }}</button>
</template>

导入Api.vue到App.vue中:通过默认导入取的别名进行调用对应的子组件的内容

<ApiVue></ApiVue>或者<ApiVue/>

import ApiVue from "./Api.vue";
<script setup>import { ref } from "vue";const msg = ref("西安");//ref 创建响应式数据// 导入Api.vue组件import ApiVue from "./Api.vue";
</script>
<template><!-- HTML代码 --><!-- <h1>北京</h1> --><h1>{{ msg }}</h1><ApiVue></ApiVue>
</template><style scoped>
/* CSS代码 */
h1 {color: red;
}
</style>

综合案列:axios.get('http://localhost:8080/article/search',{params:{...searchCondition.value}})表示通过axios进行异步请求同时通过params进行参数的传递(...searchCondition.value表示解析这个响应式对象中的数据)

工程化的使用axios的时候通过npm install axios先导入库文件

import axios from 'axios'
<script setup>
// 通过钩子函数获取所有的文章数据
import { onMounted, ref } from 'vue'
// 引入axios  npm install axios
// axios 为默认导出的 因此需要直接进行默认导入
import axios from 'axios'// 声明响应式数据
const articleList = ref([])// 发送异步请求获取文章数据
axios.get('http://127.0.0.1:8080/article/getAll')
.then(result => {// 把服务器响应的数据保存起来articleList.value = result.data
}).catch(err => {console.log(err)
});// 指定响应式数据通过v-model进行双向绑定
const searchCondition=ref({category:'',state:''
})// 为搜索按钮绑定点击事件
// function search(){
//     axios.get('http://127.0.0.1:8080/article/search?category='+searchCondition.value.category+'&state='+searchCondition.value.state)
//     .then(result => {
//         // 把服务器响应的数据保存起来
//         articleList.value = result.data
//     }).catch(err => {
//         console.log(err)
//     });
// }
const search = () => {axios.get('http://127.0.0.1:8080/article/search?category='+searchCondition.value.category+'&state='+searchCondition.value.state)// axios.get('http://localhost:8080/article/search',{params:{...searchCondition.value}}).then(result => {// 把服务器响应的数据保存起来articleList.value = result.data}).catch(err => {console.log(err)});
}
</script><template><div>文章分类: <input type="text" v-model="searchCondition.category">发布状态: <input type="text" v-model="searchCondition.state"><button @click="search">搜索</button><br /><br /><table border="1 solid" colspa="0" cellspacing="0"><tr><th>文章标题</th><th>分类</th><th>发表时间</th><th>状态</th><th>操作</th></tr><tr v-for="article in articleList"><td>{{article.title}}</td><td>{{article.category}}</td><td>{{article.time}}</td><td>{{article.state}}</td><td><button>编辑</button><button>删除</button></td></tr></table></div></template>

将对应的文章服务方法放在另外一个js文件中通过导出的形式在这个Vue中进行调用

// 导入axios
import axios from 'axios'// 定义一个变量,用于记录公共的前缀,BaseUrl
const BaseUrl = 'http://127.0.0.1:8080'
const instance = axios.create({baseURL: BaseUrl
})// 获取所有文章数据的函数
// 由于是axios的异步操作,所以不能同步获取到对应的响应的结果
// 需要同步等待服务器响应的结果并进行返回等待的结果---》
// async设定为异步  await:等待同步返回的结果(只能用在 async 异步函数中)
export async function articleGetAllService(){return await instance.get('/article/getAll').then(result => {// 把服务器响应的数据直接进行返回return result.data}).catch(err => {console.log(err)});
}// 根据文章的分类和发布状态搜索的函数
export async function articleSearchService(conditions){return await instance.get('/article/search',{params:conditions}).then(result => {// 把服务器响应的数据直接进行返回return result.data}).catch(err => {console.log(err)});
}

引入文章相关的服务函数@/表示的是src目录

<script setup>
// 通过钩子函数获取所有的文章数据
import { onMounted, ref } from 'vue'
// 引入axios  npm install axios
// axios 为默认导出的 因此需要直接进行默认导入
import axios from 'axios'
// 引入文章相关的服务函数@/表示的是src目录
import { articleGetAllService,articleSearchService } from '@/api/article'// 声明响应式数据
const articleList = ref([])// 发送异步请求获取文章数据
// 同步获取articleGetAllService函数的返回值
const getAllArticle =async function() {let data=await articleGetAllService()articleList.value=data
}
getAllArticle();// 指定响应式数据通过v-model进行双向绑定
const searchCondition=ref({category:'',state:''
})
// 为搜索按钮绑定点击事件
const search =async function() {let data=await articleSearchService({...searchCondition.value})articleList.value=data
}</script>

async设定为异步,await等待异步操作返回结果

通过如下代码可以设定指定的请求路径的前缀

// 导入axios
import axios from 'axios'// 定义一个变量,用于记录公共的前缀,BaseUrl
const BaseUrl = 'http://127.0.0.1:8080'
const instance = axios.create({baseURL: BaseUrl
})

通过一个对于异步请求axios设定一个响应的拦截器,再将axios的实例进行导出,在另一个js文件中导入就不需要处理对应的then,catch操作了

在这里插入图片描述

// 定制请求的实例
// 导入axios
import axios from 'axios'// 定义一个变量,用于记录公共的前缀,BaseUrl
const BaseUrl = 'http://127.0.0.1:8080'
const instance = axios.create({baseURL: BaseUrl
})// 添加响应的拦截器
instance.interceptors.response.use(// function (response) {//     // 对响应数据做点什么//     return response.data// },result=>{return result.data},// function (error) {//     // 对响应错误做点什么//     alert('服务器出错了')//     return Promise.reject(error)//将异步的状态转化为失败的状态// }err=>{alert('服务器出错了')return Promise.reject(err)}
)export default instance
import request from '@/util/request.js'
export function articleGetAllService(){return request.get('/article/getAll')
}// 根据文章的分类和发布状态搜索的函数
export function articleSearchService(conditions){return request.get('/article/search',{params:conditions})
}
http://www.dtcms.com/a/491584.html

相关文章:

  • 链式法则在神经网络中的应用:原理与实现详解
  • 前段模板网站南京网站开发南京乐识正规
  • K8s 核心架构是什么?组件怎么协同工作的?
  • C语言---函数
  • 做网站的费用入什么科目哈尔滨网站建设外包公司
  • YOLOv4深入解析:从原理到实践的全方位指南
  • MATLAB机器学习入门教程
  • 网站建设的好处论文网络营销以什么为中心
  • android studio设置大内存,提升编译速度
  • 从原理到实战:数据库索引、切片与四表联查全解析
  • 重庆建站免费模板mui做wap网站
  • 思想实验:如何使用MeshGPT?
  • Vue3 + TypeScript 实现 CAN 报文实时回放与合并显示
  • seo网站推广有哪些网站维护与优化教程
  • LeetCode 2598. 执行操作后的最大 MEX
  • 机器学习,深度学习,神经网络,Transformer的关系
  • 赣州市赣县区建设局网站wordpress 主题导出
  • 广州知名网站建设性价比高百度企业官网认证
  • Vue 前端面试题(含答案)大全 v2025
  • 智能化与绿色化:2025年巧克力加工设备市场发展趋势报告
  • 经营网站备案信息自己做网站 怎么赚钱
  • CLIP介绍
  • 网站文件夹结构wordpress 入侵
  • neo4j安装
  • C语言基础数组作业(冒泡算法)
  • 【芯片验证日志的艺术:如何让打印信息成为Debug的利器?】
  • 基于MCU中RTT Viwer打印,从移植到测试所遇到的问题全部解决
  • 基于mis的客户关系管理系统的设计与实现(源码+论文+部署+安装)
  • 上海定制建设网站appcan wordpress
  • php做网站浏览量深圳市宝安区区号