重要提示:
本文所用源代码为【Vue3 菜鸟入门(二)超详细:基本框架 模板语法和指令】中的内容,请先看上文
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
npm install element-plus
<el-button type="danger" v-on:click="onBtnClick()">点我el-button>
npm install axios
import axios from 'axios'
https://www.xujian.tech/atlapi/web/monitor/user/login/query/83bf11c3afbc1e6a22eb732456ff8fae
let url = "https://www.xujian.tech/atlapi/web/monitor/user/login/query/83bf11c3afbc1e6a22eb732456ff8fae";
axios.get(url).then(res=>{
console.log(res.data);
})
<template>
<div>
Hello World!
{{message}}
<el-button type="primary" v-on:click="onBtnClick()">点我el-button>
div>
template>
<script >
import axios from 'axios'
import {defineComponent} from "vue";
export default defineComponent({
// 初始执行,类似onLoad
mounted() {
console.log("Index.vue 已开始执行。");
},
// 数据管理
data(){
return {
message: "123"
}
},
// 方法管理
methods:{
onBtnClick(){
this.message = "456";
// 发起请求
let url = "https://www.xujian.tech/atlapi/web/monitor/user/login/query/83bf11c3afbc1e6a22eb732456ff8fae";
axios.get(url).then(res=>{
console.log(res.data);
})
}
}
})
script>
<style scoped>
style>
<template>
<div>
div>
template>
<script >
import axios from 'axios'
import {defineComponent} from "vue";
export default defineComponent({
// 初始执行,类似onLoad
mounted() {
console.log("UserCard.vue 已开始执行。");
},
// 数据管理
data(){
return {
}
},
// 方法管理
methods:{
},
props:[
]
})
script>
<style scoped>
style>
<template>
<div style="text-align: center;margin-left: 25%;">
<div class="bg">
<el-image
style="width: 48px; height: 48px; border-radius: 50%;"
:src="user.avatar"
:fit="fits">
el-image>
<div><el-text class="mx-1" type="success">{{user.username}}el-text>div>
<div><el-text class="mx-1" type="warning">{{user.email}}el-text>div>
<div><el-text class="mx-1" type="primary">{{outSideText}}el-text>div>
div>
div>
template>
<script >
import axios from 'axios'
import {defineComponent} from "vue";
export default defineComponent({
// 初始执行,类似onLoad
mounted() {
console.log("UserCard.vue 已开始执行。");
},
// 数据管理
data(){
return {
user:{
username: "徐先生",
email: "[email protected]",
avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80"
}
}
},
// 方法管理
methods:{
},
props:[
'outSideText'
]
})
script>
<style scoped>
.bg{
border:15px solid lightcyan;
padding: 24px;
color: black;
line-height: 42px;
width: 50%;
text-align: center;
}
style>
<template>
<div>
<UserCard outSideText="来自外部的文字">UserCard>
Hello World!
{{message}}
<el-button type="primary" v-on:click="onBtnClick()">点我el-button>
div>
template>
<script >
import axios from 'axios'
import {defineComponent} from "vue";
//引用组件
import UserCard from '../components/UserCard.vue';
export default defineComponent({
//声明组件
components:{
UserCard
},
// 初始执行,类似onLoad
mounted() {
console.log("Index.vue 已开始执行。");
},
// 数据管理
data(){
return {
message: "123"
}
},
// 方法管理
methods:{
onBtnClick(){
this.message = "456";
// 发起请求
let url = "https://www.xujian.tech/atlapi/web/monitor/user/login/query/83bf11c3afbc1e6a22eb732456ff8fae";
axios.get(url).then(res=>{
console.log(res.data);
})
}
}
})
script>
<style scoped>
style>
npm i vue-router
<script setup>
import {RouterView } from 'vue-router'
script>
<template>
<RouterView />
template>
<style scoped>
style>
// 故意不多写,你觉得该放哪儿
app.use(router)
import { createRouter,createWebHistory } from "vue-router";
import LoginVue from '../views/login/Index.vue'
import IndexVue from '../views/Index.vue'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
// 以后路由变化,只需要修改这里就行了
routes:[
{
path:'/',
name:'index',
component: IndexVue,
},
{
path:'/login',
name:'login',
component: () => import('../views/login/Index.vue')
},
]
})
export default router;
http://localhost:5173/login