当前位置:   article > 正文

vue3引入router+axios+element-plus教程_app.use(router ,createpinia(), elementplus).mount(

app.use(router ,createpinia(), elementplus).mount('#app')

一.引入vue-router

1.1在工程目录处,使用npm安装vue-router

1.2安装成功后新建router目录,创建index.js,复制如下代码

  1. import { createRouter,createWebHistory } from "vue-router";
  2. import HelloWorld from '../components/HelloWorld.vue';
  3. const router = createRouter({
  4. history:createWebHistory(),
  5. routes:[
  6. {
  7. path:'/hello',
  8. component:HelloWorld,
  9. }
  10. ]
  11. })
  12. export default router;

1.3将router挂载至全局,在main.js中加入如下代码

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import router from './router/index.js'
  4. const app=createApp(App)
  5. app.use(router)
  6. app.mount('#app')

1.4去APP.vue中进行测试,进行手动路由跳转时,引入useRouter

  1. <template>
  2. <div>
  3. <router-view></router-view>
  4. </div>
  5. </template>
  6. <script setup>
  7. import {onMounted} from 'vue'
  8.     import {useRouter} from 'vue-router'
  9.     const router=useRouter()
  10.     onMounted(()=>{
  11.         router.push("/hello")
  12.     })
  13. </script>
  14. <style>
  15. </style>

二.引入axios

2.1在工程目录处,使用npm安装element-plus

npm install axios

2.2在main.js中新增如下配置

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import router from './router/index.js'
  4. import axios from 'axios'
  5. const app=createApp(App)
  6. app.config.globalProperties.$axios=axios //挂载至全局
  7. app.use(router)
  8. app.mount('#app')

2.3在App.vue中调用试一下,引入getCurrentInstance用于获取全局变量

  1. <template>
  2. <div>
  3. <router-view></router-view>
  4. </div>
  5. </template>
  6. <script setup>
  7. import {onMounted,getCurrentInstance} from 'vue'
  8. import {useRouter} from 'vue-router'
  9. const router=useRouter()
  10.     const { $axios }=getCurrentInstance().appContext.config.globalProperties
  11.     function test(){
  12.         $axios.get("http://localhost:8081/user").then(res=>{
  13.                console.log(res)
  14.         })
  15.     }
  16. onMounted(()=>{
  17. router.push("/hello")
  18.         test()
  19. })
  20. </script>
  21. <style>
  22. </style>

三引入Element-plus

3.1安装element-plus

npminstall element-plus --save

3.2在main.js中加入如下配置,配置完之后便可以直接调用Element-plus的组件了,这是全部引入的,按需引入可以去官网查看怎么操作

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import router from './router/index.js'
  4. import ElementPlus from 'element-plus'
  5. import 'element-plus/dist/index.css'
  6. import axios from 'axios'
  7. const app=createApp(App)
  8. app.config.globalProperties.$axios=axios
  9. app.use(router)
  10. app.use(ElementPlus)
  11. app.mount('#app')

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

闽ICP备14008679号