前端基础:为什么可以直接使用axios,不需要用 Vue.use()注册?

2023-01-30 0 700

用Vue采用服务器端模块时,会加进 Vue.use() 。比如:Vue.use(VueRouter)、Vue.use(MintUI)。但用 axios时,不须要用 Vue.use(axios)就能间接采用。那这是为何呐?

即使 axios 没 install形式。

表述两个自上而下自变量

第二步,在 src 下增建 const 配置文档下 增建 const.js

. ├── src │ ├── const │ │ ├── const.js │ │ │ └── main.js └── …

第三步,怎样在 const.js 文档下,增设自变量

重点项目:default是两个第三类,倘若有两个install形式,倘若有第三模块Vue

export default { install(Vue,options){ Vue.prototype.global = { title:自上而下, isBack: true, isAdd: false, }; } }

第三步,在 main.js 下自上而下导入:(Vue.use上场

//导入自上而下自变量 import constant from ./const/const.js Vue.use(constant);

第三步,在任何人 .vue 模块中采用:

//透过js形式采用: this.global.title //或在 html 内部结构中采用 {{global.title}}

表述两个模块

1.建立如下表所示图中的配置文档和文档

前端基础:为什么可以直接使用axios,不需要用 Vue.use()注册?

2.在 Loading.vue 中表述两个模块

<template> <div class=”loading-box”> Loading… </div> </template>

3.在 index.js中 导入 Loading.vue ,并导出。

重点项目:Loading是两个第三类,倘若有两个install形式,倘若有第三模块Vue

// 导入模块 import LoadingComponent from ./loading.vue // 表述 Loading 第三类 const Loading={ // install 是默认的形式。当外界在 use 这个模块的时候,就会调用本身的 install 形式,同时传两个 Vue 这个类的模块。 install:function(Vue){ Vue.component(Loading,LoadingComponent) } } // 导出 export default Loading

4.在 main.js 中导入 loading 文档下的 index。(Vue.use上场

// 其中./components/loading/index 的 /index 能不写,webpack会自动找到并加载 index 。如果是其他的名字就须要写上。 import Loading from ./components/loading/index // 这时须要 use(Loading),如果不写 Vue.use()的话,浏览器会报错,大家能试一下 Vue.use(Loading)

5.在App.vue里面写入表述好的模块标签 <Loading></Loading>

<template> <div id=”app”> <h1>vue-loading</h1> <Loading></Loading> </div> </template>

总结

use是Vue内置的两个自上而下形式,Vue.use()接受两个第三类作为模块。第三类内部有两个install形式,会自动执行。axios是两个异步请求数据的服务器端库,内部没install形式,故不须要 Vue.use()注册登记就能间接采用。

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务