Vue脚手架搭建项目+基础知识

1. 使用脚手架创建项目

1.1 准备工作

  •         win+R,在弹出的数据框中输入cmd,数据命令查看node以及npm版本 

  •         下载vue cli

1.2 创建项目

1.2.1 创建一个英文目录文件夹,cmd打开命令命令提示符

1.2.2 vue ui命令打开控制台

1.2.3 创建项目

        创建成功

 1.3 项目结构

1.4 将项目在VSCode打开,终端运行页面

         使用命令npm run serve命令打开

1.5 修改服务端口号

        在vue.config.js中修改服务端口号,防止与后端tomcat端口冲突

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,devServer:{port: 7070}
})

         保存文件,重启项目端口号修改完毕

2. vue基础回顾

2.1 文本插值

<template><div class="hello">{{ name }}</div>
</template><script>
export default {data() {return {name: 'I LOVE YOU'}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

2.2 属性绑定

        为标签的属性绑定data方法中返回的属性

<template><div class="hello">{{ name }}<input type="name" :value="age"></div>
</template><script>
export default {data() {return {name: 'I LOVE YOU',age: 18}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

2.3 事件绑定

<template><div class="hello">{{ name }}<input type="name" :value="age"><!-- 点击事件 --><button @click="dianji()">点击</button></div>
</template><script>
export default {data() {return {name: 'I LOVE YOU',age: 18}},methods: {dianji() {alert('点击了')}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

2.4 双向绑定

        通过v-model将数据区的数据与输入框数据双向绑定

<template><div class="hello">{{ name }}<input type="name" :value="age"><!-- 点击事件 --><button @click="dianji()">点击</button><input type="text" v-model="name"></div>
</template><script>
export default {data() {return {name: 'I LOVE YOU',age: 18}},methods: {dianji() {alert('点击了')}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

2.5 条件渲染

<template><div class="hello"><div v-if="sex==1">男</div><div v-else-if="sex==2">女</div><div v-else>未知</div></div>
</template><script>
export default {data() {return {sex:2}},methods: {}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

2.6 axios

        Axios是一个基于promise的网络请求库,作用于浏览器和node.js中,用于发ajax异步请求,实现前后端交互

2.6.1 下载axios

npm install axios

2.6.2 在项目中使用axios

2.6.3 axios的API

  • url:请求路径
  • data:请求体数据,最常见的是JSON格式
  • config:配置对象,可以设置查询参数,请求头信息

 2.6.4 axios请求的跨域问题

        正常情况下,前端数据一个服务,后端属于一个服务,在不同的域里,所以会出现跨域问题,解决方法就是在vue.config.js文件中配置代理

<template><button @click="request11()"></button></div>
</template><script>
// 导入axios
import axios from 'axios'export default {data() {return {sex:2}},methods: {request11(){axios.post('http://localhost:8080/user/login',{username:'admin', // json数据password:'123456'}).then(res=>{ // 成功回调console.log(res.data)}).catch(err=>{ // 错误回调console.log(err)})}}
}
</script>
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,devServer:{port: 7070,proxy:{// 配置请求代理,用于将特定请求转发到后端服务器,解决跨域问题'/api':{// 将请求转发到的目标服务器地址target:'http://localhost:8080',changeOrigin:true, // 是否更改请求的来源,通常用于处理虚拟主机的情况pathRewrite:{'^/api':'' // 移除请求路径中的 '/api' 前缀,以便目标服务器正确处理请求}}}}
})

2.6.5 发送请求

  • 方式一
<template><div class="hello"><div v-if="sex==1">男</div><div v-else-if="sex==2">女</div><div v-else>未知</div><button @click="request11()">按钮</button></div>
</template><script>
// 导入axios
import axios from 'axios'export default {data() {return {sex:2}},methods: {request11(){  axios.post('http://localhost:8080/user/login',{username:'admin', // json数据password:'123456'}).then(res=>{ // 成功回调console.log(res.data)}).catch(err=>{ // 错误回调console.log(err)})},req(){axios.get('http://localhost:8080/user/getUserById',{// 携带请求头headers:{token:'xxxxxxxxxxxxxxxxxxxxxxxxxx'}}).then(res=>{console.log(res.data)}).catch(err=>{console.log(err)})}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
  • 方式二
<button @click="handleSend()"></button>handleSend(){axios({url:'http://localhost:8080/user/login',method:'post',data:{username:'admin',password:'<PASSWORD>'}}).then(res=>{console.log(res.data)axios({url:'http://localhost:8080/user/info',method:'get',headers:{token:res.data.data.token}})}).catch(err=>{console.log(err)})}

3. 路由Vue-Router

        vue属于单页面应用,路由的作用就是根据浏览器路径不同,用不同的视图组件替换掉这个页面内容。实现不同的路径,对应不同的页面展示

3.1 创建带有路由的项目

        创建好之后,package.json中有vue-router的依赖

        main.js中引入了router,说明这个项目具备了路由功能

3.2 路由配置

路由组成:

  • VueRouter:路由器,根据路由请求在路由视图中动态渲染对应的视图组件
  • <router-link>:路由链接组件,浏览器会解析成<a>
  • <router-view>:路由视图组件,用来展示与路由路径匹配的视图组件

  • index.js中维护路由

  •  App.vue,<router-link>标签生成超链接

  • <router-view>:路由视图组件不能删掉!!!

       路由视图组件起到的是占位符的作用

3.3 路由跳转

有两种方式:

  • 标签式
  • 编程式

标签式就是上面演示的那种,编程式如下:

<template><div id="app"><nav><router-link to="/">Home</router-link> |<router-link to="/about">About</router-link><input type="button" value="编程式跳转" @click="jump()"></nav><!-- <router-view>:路由视图组件 --><router-view/></div>
</template><script>
export default {methods: {jump() {this.$router.push('/about')}}
}
</script><style>
#app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;
}nav {padding: 30px;
}nav a {font-weight: bold;color: #2c3e50;
}nav a.router-link-exact-active {color: #42b983;
}
</style>

        当用户输入的路径不对时,都会重定向到404页面,一张图告诉你如何重定向到不存在的页面:

4. 嵌套路由

        嵌套路由是组件内要切换内容,就需要用到嵌套路由(子路由)

4.1 案例

        以以下这个案例给大家分析:

实现步骤:

1)安装并导入elementui,实现页面布局(container布局容器)--ContainerView.vue

  • 下载

使用命令下载elementui:npm i element-ui -S

  • 在main.js中导入elementui
import Vue from 'vue'
import App from './App.vue'
import router from './router'
// 导入elementui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';Vue.config.productionTip = false
// 全局使用elementui
Vue.use(ElementUI);new Vue({router,render: h => h(App)
}).$mount('#app')
  •  创建视图组件
<template><el-container><el-header>Header</el-header><el-container><el-aside width="200px">Aside</el-aside><el-main>Main</el-main></el-container>
</el-container>
</template><script>
export default {}
</script><style>.el-header, .el-footer {background-color: #B3C0D1;color: #333;text-align: center;line-height: 60px;}.el-aside {background-color: #D3DCE6;color: #333;text-align: center;line-height: 200px;}.el-main {background-color: #E9EEF3;color: #333;text-align: center;line-height: 160px;}body > .el-container {margin-bottom: 40px;}.el-container:nth-child(5) .el-aside,.el-container:nth-child(6) .el-aside {line-height: 260px;}.el-container:nth-child(7) .el-aside {line-height: 320px;}
</style>
  • 在main.js中配置路由跳转

2)提供子视图组件,用于效果展示

  • P1View.vue
<template><div>这是P1View</div>
</template><script>
export default {}
</script><style>
</style>
  •  P2View.vue
<template><div>这是P2View</div>
</template><script>
export default {}
</script><style>
</style>
  •  P3View.vue
<template><div>这是P3View</div>
</template><script>
export default {}
</script><style>
</style>

3)在src/router/index.js中配置路由映射规则(嵌套路由配置)

import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'Vue.use(VueRouter)const routes = [// 维护路由表,某个路由路径对应哪个视图组件{path: '/',name: 'home',// 静态加载component: HomeView},{path: '/about',name: 'about',// route level code-splitting// this generates a separate chunk (about.[hash].js) for this route// which is lazy-loaded when the route is visited.// 懒加载component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')},{path: '/test',component: () => import( '../views/404View.vue')},{path: '/c',component: () => import( '../views/container/containerView.vue'),// 重定向到p1redirect: '/c/p1',children:[{path: '/c/p1',component: () => import( '../views/container/P1View.vue')},{path: '/c/p2',component: () => import( '../views/container/P2View.vue')},{path: '/c/p3',component: () => import( '../views/container/P3View.vue')}]},{path:"*",redirect: '/404'}
]const router = new VueRouter({routes
})export default router

4)在布局容器视图中添加<router-view>,实现子视图组件展示

<template><el-container><el-header>Header</el-header><el-container><el-aside width="200px">Aside</el-aside><el-main><!-- 占位符 --><router-view/></el-main></el-container>
</el-container>
</template>

5)在布局容器视图中添加<router-link>,实现路由请求

<template><el-container><el-header>Header</el-header><el-container><el-aside width="200px"><!-- 路由请求:生成超链接 --><router-link to="/c/p1">P1</router-link><br><router-link to="/c/p2">P2</router-link><br><router-link to="/c/p3">P3</router-link></el-aside><el-main><!-- 占位符 --><router-view/></el-main></el-container>
</el-container>
</template><script>
export default {}
</script><style>.el-header, .el-footer {background-color: #B3C0D1;color: #333;text-align: center;line-height: 60px;}.el-aside {background-color: #D3DCE6;color: #333;text-align: center;line-height: 200px;}.el-main {background-color: #E9EEF3;color: #333;text-align: center;line-height: 160px;}body > .el-container {margin-bottom: 40px;}.el-container:nth-child(5) .el-aside,.el-container:nth-child(6) .el-aside {line-height: 260px;}.el-container:nth-child(7) .el-aside {line-height: 320px;}
</style>

5. 状态管理vuex

5.1 介绍

  • vuex是一个专门为Vue.js应用程序开发的状态管理库
  • vuex可以在多个组件之间共享数据,并且共享的数据是响应式的,即数据的变更能即使渲染到模板
  • vuex采用集中式存储管理所有组件的状态

5.2 vuex核心概念

5.3 使用方式

5.3.1 创建携带vuex的项目

  • index.js
import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)// 集中管理多个组件共享的数据
export default new Vuex.Store({state: {},getters: {},mutations: {},actions: {},modules: {}
})
  • main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store'Vue.config.productionTip = falsenew Vue({store, // 创建vuex实例render: h => h(App)
}).$mount('#app')

5.3.2 定义展示共享数据

5.3.3 在mutations中定义函数,修改共享数据

5.3.4 在actions中定义函数,用于调用mutation

// import { set } from 'core-js/core/dict'
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'Vue.use(Vuex)// 集中管理多个组件共享的数据
export default new Vuex.Store({// 共享数据state: {name:'未登录游客'},getters: {},// 修改共享数据只能通过mutation实现,必须是同步操作mutations: {setName(state, name){state.name = name}},// 封装异步操作,不能直接修改共享数据,只能通过mutation来实现actions: {setNameByAxios(context){axios({url:"/api/getUserInfo",method:"POST",data:{username:"admin",password:"123456"}}).then(res=>{if(res.data.code == 1){// 异步请求后,需要修改共享数据,只能通过mutation来实现// 在action中调用mutation中定义的setName方法context.commit("setName",res.data.username)}})}},modules: {}
})

       在vue.config.js 配置重定向

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,devServer: {port: 8081,proxy: {// 将含有/api的字段重定向成 http://localhost:8080'/api': {target: 'http://localhost:8080',changeOrigin: true,pathRewrite: {// 重写路径,将含有/api的字段替换成 (空)'^/api': ''}}}}
})

6. TypeScript

6.1 TypeScript介绍

  • TypeScript是微软推出的开源语言

  • TypeScript是JavaScript的超集(js有的ts都有)

  • TypeScript在js基础上做了类型支持

  • TypeScript文件扩展名为ts

  • TypeScript可编译成标准的JavaScript,并且在编译时进行类型检查

6.2 TypeScript常用类型

6.2.1 创建携带TypeScripe的项目

        类型标注的位置:

  • 标注变量
  • 标注参数
  • 标注返回值

6.2.2 字符串类型、数据类型、布尔类型

// 字符串类型
let str: string = 'I LOVE YOU 1314';// 数字类型
let num: number = 1314;// 布尔类型
let bool: boolean = true;console.log(str, num, bool);console.log("-------------------------");// 字面量类型
function printTest(s:string,alignment:'left'|'center'|'right'){console.log(s,alignment)
}
// 调用函数
printTest('I LOVE YOU 1314','left');console.log("-------------------------");// interface 接口类型
interface Cat{name:string;// ? 可选属性age?:number;
}
// 定义变量,并且指定为Cat类型
const c1:Cat = {name:'Tom',age:10}
const c2:Cat = {name:'Tom'}console.log("-------------------------");// class类 - 基本使用
class User{name:string;constructor(name:string){this.name = name;}study(){console.log('I am '+this.name);}
}
// 使用User类型
const user = new User('Tom');// 输出类中的属性
console.log(user.name);// 调用类中的方法
user.study();console.log("-------------------------");// class类 - 实现接口
interface Animal{name:string;eat():void;
}// 定义一个类实现Animal接口
class Bird implements Animal{name:string;constructor(name:string){this.name = name;}eat():void{console.log('I am eating');}
}
//  创建类型为Bird的对象
const bird = new Bird('小鸟');
console.log(bird.name);
bird.eat();// class类 - 继承Bird类
class Parrot extends Bird{say(){console.log(this.name+' can say');}
}
const parrot = new Parrot('鹦鹉');
console.log(parrot.name);
parrot.eat();
parrot.say();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/89890.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/89890.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序下单页—地址列表页—新增地址页 页面交互

新增地址流程&#xff1a; 下单页 → 地址列表页 (1次跳转)地址列表页 → 新增地址页 (1次跳转)保存地址 → 返回地址列表页 (1次返回&#xff0c;自动刷新列表) 选择地址流程&#xff1a; 地址列表页 → 选中地址 → 返回下单页 (1次返回) 更换地址&#xff1a; 下单页 → 地址…

JVM与JMM

为了更清晰地对比JVM和JMM&#xff0c;我们可以采用表格形式&#xff0c;从定义、功能、结构、与多线程关系等方面进行详细比较&#xff1a; 对比项JVM&#xff08;Java Virtual Machine&#xff09;JMM&#xff08;Java Memory Model&#xff09;定义一种虚构的计算机&#x…

【Docker基础】Docker数据卷管理:docker volume rm及其参数详解

目录 1 引言&#xff1a;Docker Volume 的生命周期管理 2 docker volume rm命令基础 2.1 命令作用 2.2 命令语法 3 参数深度解析 3.1 基础参数表 3.2 高级参数详解 3.2.1 --force&#xff08;-f&#xff09; 4 Volume删除前置条件 4.1 可删除状态判断 4.2 常见报错处…

嵌入式系统内核镜像相关(十)

文章目录 前言一、点亮多个led灯的基础实验以及其中的问题1.1 基础流程1.1.1 alinx教程的问题1.1.1.1 驱动程序中的亮/灭逻辑修改&#xff01;1.1.1.1.1 逻辑错误的修改1.1.1.1.2 多灯亮/灭 1.1.1.2 驱动程序中引脚的问题以及与裸机开发的区别&#xff08;重要&#xff09;1.1.…

Word和Excel批量转PDF新方法,操作简单

PDF是一种跨平台的文档格式&#xff0c;无论在任何设备上查看&#xff0c;其排版、字体和图像都不会发生变化。这确保了文档的一致性&#xff0c;避免了由于不同软件版本或操作系统引起的显示问题。这款小巧的工具大小不到2MB&#xff0c;使用起来异常简单。只需要把需要转换的…

AI搜索 MCP最佳实践

背景 那些 LLM 不知道的事 尝试直接询问LLM“今天天气如何”时&#xff0c;会发现LLM无法回答——它既不知道“今天”是哪天&#xff0c;也无法获取地理位置信息。这揭示了LLM的局限&#xff1a;缺乏与外部工具和实时数据的交互能力。 为解决这一问题&#xff0c;MCP&#x…

JVM 简介与作用

&#x1f680; JVM 简介与作用 &#x1f4da; 深入理解 Java 虚拟机的核心概念与重要作用 &#x1f4d6; 目录 &#x1f914; 什么是 Java 虚拟机&#xff08;JVM&#xff09;&#x1f310; JVM 在 Java 生态中的核心地位&#x1f500; JVM 跨平台原理剖析&#x1f4dd; 总结 …

✨ OpenAudio S1:影视级文本转语音与语音克隆Mac整合包

✨ OpenAudio S1&#xff1a;影视级文本转语音与语音克隆Mac整合包 &#x1f680; OpenAudio S1 简介 OpenAudio S1 是由 Fish Audio 开发的 Fish Speech 系列的最新一代人工智能语音生成模型。该模型旨在大幅提升 AI 语音生成的技术水平&#xff0c;为用户提供更加自然、富有表…

spring加载外部properties文件属性时,读取到userName变量值和properties文件的值不一致

问题 使用spring DI注入外部properties文件属性时&#xff0c;读取到userName变量值和properties文件的值不一致。 bean属性注入&#xff1a; <!--加载配置文件--> <context:property-placeholder location"classpath:*.properties"/><bean id"…

黑马点评系列问题之基础篇p7 06初识redis无法在虚拟机查到图形化界面存进去的键

问题描述 在RESP中输入了一些键(name,age等这些) 但是在图形化界面里面输入的&#xff0c;在非图形化界面就找不到&#xff0c;在非图形化界面里输入的&#xff0c;在图形化界面里就可以查到。 原因分析及解决 经过多次实验&#xff0c;发现是因为在添加键名的时候&#xff0…

在VMware虚拟机中安装Windows 98时,Explorer提示“该程序执行了非法操作,即将关闭”的解决办法

在使用iso文件&#xff08;MD5: 0E496B5DCC519F550AAF0BCFBB4A11EA&#xff09;安装Windows98时&#xff0c;遇到此提示。 虽然原因未知&#xff0c;也无需深入探究&#xff0c;但是根据网友在 https://www.bilibili.com/opus/435866522585702782 中给出的相似经验&#xff…

在浏览器中使用SQLite(官方sqlite3.wasm)

有人可能会问&#xff1a;既然浏览器里又内置得IndexedDB&#xff0c;而且在IndexedDB里存数据&#xff0c;关了浏览器数据也不会丢&#xff0c;为什么还要在浏览器里用SQLite? 实际上&#xff0c;当 IndexedDB 内的数据量增多&#xff0c;数据和数据之间的关系变得复杂&…

数据结构(Java)--位运算

前言 本文为本小白学习数据结构的笔记&#xff0c;将以算法题为导向&#xff0c;向大家更清晰的介绍数据结构相关知识&#xff08;算法题都出自B站马士兵教育——左老师的课程&#xff0c;讲的很好&#xff0c;对于想入门刷题的人很有帮助&#xff09; 为什么要使用为位运算 位…

秋招Day14 - Redis - 应用

Redis如何实现异步消息队列&#xff1f; List配合LPUSH和RPOP。 另外就是用 Redis 的 Pub/Sub 来实现简单的消息广播和订阅。 但是这两种方式都是不可靠的&#xff0c;因为没有 ACK 机制所以不能保证订阅者一定能收到消息&#xff0c;也不支持消息持久化。 Redis如何实现延时…

因果语言模型、自回归语言模型、仅解码器语言模型都是同一类模型

因果语言模型、自回归语言模型、仅解码器语言模型都是同一类模型 flyfish 因果语言模型&#xff08;causal Language Models&#xff09; 自回归语言模型&#xff08;autoregressive language models&#xff09; 仅解码器语言模型&#xff08;decoder-only language models&am…

jvm架构原理剖析篇

简单题&#xff08;5道&#xff09; 考查内容&#xff1a;JVM运行时数据区域 题干&#xff1a;Java虚拟机栈的主要作用是&#xff1f; A. 存储对象实例 B. 存储方法调用和局部变量 C. 存储静态字段 D. 存储字节码指令 正确答案&#xff1a;B 解析&#xff1a;虚拟机栈用于存储方…

智链万物:人工智能驱动的产业智能化革命

当生成式AI在艺术与创意领域掀起风暴&#xff0c;大型语言模型重塑信息交互方式时&#xff0c;一场更为基础、影响更为深远的变革&#xff0c;正在全球实体经济的根基处悄然发生并加速推进——这就是产业智能化。它并非简单的“机器换人”&#xff0c;而是人工智能&#xff08;…

python中上下文管理器 与 try finally有什么区别

目录 主要区别代码对比何时使用哪种方式 主要区别 语法简洁性 上下文管理器使用 with 语句&#xff0c;语法更简洁优雅try-finally 需要显式编写异常处理代码&#xff0c;更冗长 代码复用性 上下文管理器可以封装为类或函数&#xff0c;便于在多处复用try-finally 通常需要在每…

人体属性识别+跌倒检测:儿童行为监测与安全升级

智慧幼儿园的AI智能检测盒应用实践 背景&#xff1a;传统园区管理的三大痛点 传统幼儿园管理长期面临三大核心挑战&#xff1a;一是安全监控依赖人工巡查&#xff0c;存在视觉盲区与响应延迟&#xff0c;如某连锁幼儿园曾因人工巡查疏漏&#xff0c;导致3起儿童跌倒事故未能及…

【ESP32-IDF笔记】09-UART配置和使用

环境配置 Visual Studio Code &#xff1a;版本1.98.2 ESP32&#xff1a;ESP32-S3 ESP-IDF&#xff1a;V5.4 支持型号&#xff1a;ESP32、ESP32-C2、ESP32-C3、ESP32-C5、ESP32-C6、ESP32-C61、ESP32-H2、ESP32-P4、 ESP32-S2、ESP32-S3 简介 通用异步接收器/发送器 (UART) …