vue.2.0-自定义全局组件

App.vue

<template>
  <div id="app">
    <h3>welcome vue-loading</h3>
    <Loading></Loading>    <!--<Loading></Loading>是自定义组件-->   </div> </template>

main.js

import Vue from ‘vue‘
import App from ‘./App.vue‘

import Loading from ‘./components/loading‘  //定义Loading,components、loading是一个文件夹,loading里面必须要index.js

Vue.use(Loading)    //use Loading

new Vue({
    el: ‘#app‘,
    render: h => h(App)
})

index.js

import LoadingComponent from ‘./Loading.vue‘

const Loading = {     //定义Loading
    install: function(Vue) {//install是必须的
        Vue.component(‘Loading‘, LoadingComponent)//定义一个组件
    }
};

export default Loading

Loading.vue

<template>
    <div class="loading-box">
        {{msg}}
    </div>
</template>
<script>
    export default{
        data(){
            return {
                msg:‘Loading...^_^‘
            }
        }
    }
</script>
<style scoped>
    .loading-box{
        color: red;
        font-size: 40px;
        font-family: 微软雅黑;
        text-shadow: 2px 2px 5px #000;
    }
</style>
时间: 2024-08-08 14:54:34

vue.2.0-自定义全局组件的相关文章

vue.js2.0 自定义组件初体验

理解 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.在有些情况下,组件也可以是原生 HTML 元素的形式,以 is 特性扩展. 使用组件 创建单文件组件 <template> </template> <script> export default { }; </script> <style lang="st

vue.js中的全局组件和局部组件

组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能. 组件的使用有三个步骤: 1.创建组件构造器 2.注册组件 3.使用组件 先来看一段代码: <!DOCTYPE html> <html> <body> <div id="app"> <!-- 3. #app是Vue实例挂载的元素,应该在挂载元素范围内使

vue 复习篇. 注册全局组件,和 组件库

初篇 ============================================================== 1. 编写loading组件(components/Loading/index.vue) <template> <div>loading</div> </template> <script> export default { name: 'loading' } </script> 2.注册为全局组件 (c

Vue系列之 =&gt; 自定义全局指定让文本框自动获取焦点

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatib

Vue 2.0 路由全局守卫

路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫(navigation-guards)这个名字,听起来怪怪的,但既然官方文档是这样翻译的,就姑且这么叫吧. 贴上文档地址:https://router.vuejs.org/zh-cn/advanced/navigation-guards.html 全局守卫 你可以使用 router.beforeEa

Vue自定义插件(组件)Loading

vue.use()方法可以用来注册组件或者插件. 只要传入一个install()方法既可以注册 install(Vue,option){} 可以通过几种方式来自定义开发 Vue.$loading = -//直接挂载在Vue类上 Vue.propertype.$loading = -//直接挂载在Vue原型链上,可以通过this.$loading调用 Vue.component()//注册一个全局组件 Vue.directive()//注册全局指令 Vue.mixin()//全局混入,可以理解为继

v-once指令、v-cloak指令、条件指令家族、原义指令、循环指令、todolist案例、实例成员-符号、实例成员-计算属性、实例成员-属性监听、监听的案例、局部组件、全局组件、组件交互(父传子、子传父)

v-once指令: v-once:单独使用,限制的标签内容一旦赋值,便不可被动更改(如果是输入框,可以主动修改) <div id="app"> <input type="text" v-model="msg"> <!-- 一旦赋值,只可主动更改 --> <input type="text" v-model="msg" v-once> <p>{{ m

Vue2.x 全局组件

App.vue: 注册全局组件: import toastr from './common-componetnt/toastr/toastr.vue' Vue.component('toastr', toastr); 组件里边使用:<template> <toastr ref="toastr"></toastr> </template> script: this.$refs.toastr.success("成功")

自定义vue全局组件use使用、vuex的使用

自定义vue全局组件use使用(解释vue.use()的原理)我们在前面学习到是用别人的组件:Vue.use(VueRouter).Vue.use(Mint)等等.其实使用的这些都是全剧组件,这里我们就来讲解一下怎么样定义一个全局组件,并解释vue.use()的原理而我们再用Axios做交互,则不能使用Vue.use(Axios),因为Axios没有install 自定义一个全局Loading组件,并使用:总结目录: |-components |-loading |-index.js 导出组件,