一、
1、vue介绍
vue是一款敏捷开发的前端框架。
2、vue基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="vue3.js"></script>
</head>
<body>
<div id="app">
<!--事件绑定-->
<!--<p @click="foo">消息:{{msg}}</p>-->
<p>消息:{{msg}}</p>
<p><span v-html="link"></span></p>
<p></p><input type="text" v-model="msg"></p>
<!--单向的不会随一个改变而改变
<a :href="link2">{{data2}}</a><input v-bind:value="link2"><input :value="data2">-->
<a :href="link2">{{data2}}</a><input v-model="link2"><input v-model="data2">
</div>
<script>
var vm=Vue.createApp({
data(){
return {
msg:"this is yuan",
x:100,
link:"<a target='_blank' href='http://www.baidu.com'>百度</a>",
link2:"http://www.baidu.com",
data2:"百度",
}
},
/* methods:{
foo(){
console.log(this.msg);
this.msg="hello tian"
}
}*/
}).mount("#app");
</script>
</body>
</html>
3、class
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="vue3.js"></script>
<style>
.c1{
color:red;
}
.c2{
background: lightpink;
}
</style>
</head>
<body>
<div id="vm1">
<!--单独用法
<p :class="{c1:true,c2:false}">this is a test</p>-->
<p :class="cssObj">this is a test</p>
</div>
<script>
var vm1=Vue.createApp({
data(){
return{
cssObj:{
c1:true,
c2:false
}
}
}
}
).mount("#vm1")
</script>
</body>
</html>
4 、v-if\v-for\computed属性
模版内的表达式是为了简单计算,假如多次利用计算就会使框架变得特别的沉重。用于响应式设计的复杂逻辑需要使用computed属性。
method的方法没有缓存,每次调用都会再重新执行一遍;computed属性方法有缓存,只有在data值更改的时候才会重新计算,否则返回缓存值,速度相对来说computed快一些。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="vue3.js"></script>
</head>
<body>
<div id="vm">
<table>
<tr>
<th>序号</th>
<th>书名</th>
<th>价格</th>
</tr>
<!-- 以下功能借用了computed属性,推荐这样用-->
<tr v-for="(book,i) in foo" >
<td>{{i}}</td>
<td>{{book["name"]}}</td>
<td>{{book["price"]}}</td>
</tr>
<!-- 由于if优先级比for高,因此放在一个标签里会先执行if.以下虽然能够实现功能,但是由于新建了一些tr空标签因此不推荐使用。-->
<!-- <tr v-for="(book,i) in books" >
<template v-if="book.price>1000">
<td>{{i}}</td>
<td>{{book["name"]}}</td>
<td>{{book["price"]}}</td>
</template>
</tr>-->
</table>
</div>
<script>
var vm1=Vue.createApp({
data(){
return{
books:[{name:"红楼梦",price:1000},{name:"三国演义",price:1900},{name:"西游记",price:899},{name:"水浒传",price:600}],
}
},
computed:{
foo(){
return this.books.filter(function(item){
if(item.price>1000){
return item;
}
})
}
}
}).mount("#vm")
</script>
</body>
</html>
5、watch侦听
监听数值变化,传参会自动传两个值,分别是变化前和变化后的参数,num是属性的值
<p>{{num}}</p> watch: { num: function (newval, oldval) { //num发生变化的时候,要执行的代码 console.log(`num已经从${oldval}变成${newval}`); } }
<input type="password" :class="tips" v-model="password">
watch: {
password() {
if (this.password.length > 6 && this.password.length < 16) {
this.tips = "is_pass"
} else {
this.tips = "is_fail";
}
}
}
6、生命周期
beforeCreate created beforeMount mounted beforeUpdate updated
二、ajax请求
异步操作,页面只是局部更新,并不重新加载。
<button @click="get_weather">获取天气</button>
methods: {
get_weather() {
// 发送http请求获取天气
axios.get("http://wthrcdn.etouch.cn/weather_mini", {
params: {
city: this.city,
}
}).then(response => {
console.log(response.data.data.forecast);
this.weather_list = response.data.data.forecast;
}).catch(error => {
console.log(error);
})
},
showFengLi(content) {
return content.replaceAll("<![CDATA[", "").replaceAll("]]>", "");
},
},
created(){
this.get_weather()
}
三、VUE脚手架
vue/cli一个后端的程序,安装前先安装node.js否则无法安装成功。node.js建议安装10以上版本,否则可能会无法安装vue/cli4以上版本,目前使用vue3.以下是安装vue/cli最新版本的方式
npm install -g @vue/cli
安装完检测方式
vue --version
如果想要升级全局vue/cli包
npm update -g @vue/cli
2、创建项目
vue create 项目名称
3、导入组件ajax axios
在工程下的terminal执行如下命令
npm install axios --save-dev
4、子组件传递数据给父组件
自定义事件是父组件事件
$emit(自定义事件,参数1,参数2)
watch: { city() { alert(this.city) this.$emit("getCity", this.city); } },
父类:
<Nav @getCity="getCity"></Nav> methods: { getCity: function (city) { console.log(city) this.city = city; }, }
5、父组件数据传递给子组件
父组件:
<Forecast :choose_city="city"></Forecast>
子组件:
props: { // 接收来自父组件的数据 choose_city: { default:"北京", type: String, } },
6、项目打包
npm run build
在打包之后项目中出现 dist
目录,dist
目录就是 Vue脚手架项目的生产目录(直接部署目录)。
7、ant-design前端框架实现案例
<template>
<a-layout style="min-height: 100vh">
<a-layout-sider v-model:collapsed="collapsed" collapsible>
<div class="logo" />
<a-menu v-model:selectedKeys="selectedKeys" theme="dark" mode="inline" v-for="menu in menu_list">
<a-menu-item v-if="menu.children.length===0" v-model:key="menu.id">
<router-link :to="menu.menu_url">
<pie-chart-outlined />
<span>{{menu.title}}</span>
</router-link>
</a-menu-item>
<a-sub-menu key="menu.id" v-else>
<template #title v-if="menu.icon=='user-outlined'">
<span>
<user-outlined />
<span>{{ menu.title }}</span>
</span>
</template>
<template #title v-else-if="menu.icon=='team-outlined'">
<span>
<team-outlined />
<span>{{ menu.title }}</span>
</span>
</template>
<template #title v-else>
<span>
<file-outlined />
<span>{{ menu.title }}</span>
</span>
</template>
<a-menu-item v-for="echild in menu.children" :key="echild.id">
<router-link :to="echild.menu_url">{{echild.title}}</router-link>
</a-menu-item>
</a-sub-menu>
</a-menu>
</a-layout-sider>
<a-layout>
<a-layout-header style="background: #fff; padding: 0" />
<a-layout-content style="margin: 0 16px">
<router-view/>
</a-layout-content>
<a-layout-footer style="text-align: center">
Ant Design ©2018 Created by Ant UED
</a-layout-footer>
</a-layout>
</a-layout>
</template>
<script>
import 'ant-design-vue/dist/antd.css';
import { PieChartOutlined, DesktopOutlined, UserOutlined, TeamOutlined, FileOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref } from 'vue';
export default defineComponent({
components: {
PieChartOutlined,
DesktopOutlined,
UserOutlined,
TeamOutlined,
FileOutlined,
},
data() {
return {
collapsed: ref(false),
selectedKeys: ref(['1']),
menu_list: [
{
id: 1, icon: 'mail', title: '展示中心', tube: '', 'menu_url': '/show', children: []
},
{
id: 2, icon: 'mail', title: '资产管理', 'menu_url': '/home', children: []
},
{
"id": 'sub1', icon: 'user-outlined', title: '入库管理', tube: '', children: [
{id: 4, icon: 'mail', title: '执行任务', 'menu_url': '/show'},
{id: 5, icon: 'mail', title: '命令管理', 'menu_url': '/home'},
]
},
{
id: 'sub2', icon: 'team-outlined', title: '出库管理', tube: '', children: [
{id: 6, title: '出库主页', menu_url: '/home'},
{id: 7, title: '发布申请', menu_url: '/show'}
]
},
{
id: 'sub3', icon: 'file-outlined', title: '代码发布', tube: '', children: [
{id: 8, title: '应用管理', menu_url: '/home'},
{id: 9, title: '发布申请', menu_url: '/show'}
]
}
]
}
},
});
</script>
<style>
#components-layout-demo-side .logo {
height: 32px;
margin: 16px;
background: rgba(255, 255, 255, 0.3);
}
.site-layout .site-layout-background {
background: #fff;
}
[data-theme='dark'] .site-layout .site-layout-background {
background: #141414;
}
</style>
作为联系参考,也可作为实现指南