vue.js注册引用全局消息组件

1 注册

在 src/components 下新建 index.js 文件,复制贴入以下代码:

注册全局组件需要使用 Vue.component,第一个参数 ‘Message‘ 是组件名称,第二个参数 Message 是一个对象或者函数,我们这里是一个对象。(vue-cli模板下)

import Message from ‘./Message‘

Vue.component(‘Message‘, Message)

2 引用

打开 src/main.js 文件,引入 ./components

import Vue from ‘vue‘
import App from ‘./App‘
import router from ‘./router‘
import ‘./directives‘
import ‘./components‘

我们通过引入 ./components/index.js,执行其中代码,就可以使用其内部注册的所有组件了。

3 使用

在目标组件的 data 中添加了 3 个消息组件相关的属性 msgmsgType 和 msgShow

<script>
import createCaptcha from ‘@/utils/createCaptcha‘
import ls from ‘@/utils/localStorage‘

export default {
  name: ‘Register‘,
  data() {
    return {
      captchaTpl: ‘‘, // 验证码模板
      username: ‘‘, // 用户名
      password: ‘‘, // 密码
      cpassword: ‘‘, // 确认密码
      captcha: ‘‘, // 验证码
      msg: ‘‘, // 消息
      msgType: ‘‘, // 消息类型
      msgShow: false // 是否显示消息,默认不显示
    }
  },

然后在 methods 中添加了一个 showMsg 的方法用来改变当前的消息;

  methods: {
    login(user) {
          ls.setItem(‘user‘, user)
          this.showMsg(‘注册成功‘, ‘success‘)
    },
    showMsg(msg, type = ‘warning‘) {
        this.msg = msg
        this.msgType = type
        this.msgShow = false

        this.$nextTick(() => {
            this.msgShow = true
        })
    }
  }
}
</script>

原文地址:https://www.cnblogs.com/KulaAi/p/10280168.html

时间: 2024-07-30 10:42:12

vue.js注册引用全局消息组件的相关文章

基于vue.js的图片预览组件2.0.1

基于vue.js的图片预览组件 Github github 安装 npm install enlargeimg --save-dev import enlargeimg from 'enlargeimg'; 基础用法 <enlargeImg :data="files"></enlargeImg> export default { name: 'hello', data () { return { msg: 'Welcome to Your Vue.js App'

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

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

Vue.js注册组件

<!DOCTYPE html><html><head><meta charset="utf-8"> <title>vue.js Hello World!</title><script type="text/javascript" src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> &

Vue.js 注册组件

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" con

vue.js基础知识篇(6):组件详解

第11章:组件详解 组件是Vue.js最推崇也最强大的功能之一,核心目标是可重用性. 我们把组件代码按照template.style.script的拆分方式,放置到对应的.vue文件中. 1.注册 Vue.js的组件注册分为全局注册和局部注册. 全局注册使用Vue.component方法.第一个参数是组件名字,第二个参数是组件的构造函数,要么是function,要么是object. <!DOCTYPE html> <html lang="en"> <hea

Vue.js 系列教程 2:组件,Props,Slots

原文:intro-to-vue-2-components-props-slots 译者:nzbin 这是关于 JavaScript 框架 Vue.js 五个教程的第二部分.在这一部分,我们将学习组件,Props 以及 Slots.这个系列教程并不是一个完整的用户手册,而是通过基础知识让你快速了解 Vuejs 以及它的用途. 系列文章: 渲染, 指令, 事件 组件, Props, Slots (你在这!) Vue-cli Vuex 动画 组件和传递数据 如果你熟悉 React 或者 Angular

Vue.js 实例和内置组件 入门

首先来看看实例的概述: 实例就是在构造器外部操作操作构造器内部的属性和方法. 实例的作用就是给Vue提供与其它js及其框架结合使用的接口. 进入实例阶段: 首先来看 Vue.js 搭配 jQuery 使用: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Early Examples Demo</title&g

vue.js学习笔记(Vuejs——组件——props数据传递)

①组件实例的作用域: 是孤立的,简单的来说,组件和组件之间,即使有同名属性,值也不共享. <div id="app"> <add></add> <del></del> </div> <script> var vm = new Vue({ el: '#app', components: { "add": { template: "<button>btn:{{btn

vue.js(18)--父组件向子组件传值

子组件是不能直接使用父组件中数据的,需要进行属性绑定(v-bind:自定义属性名=“msg”),绑定后需要在子组件中使用props[‘自定义属性名’]数组来定义父组件的自定义名称. props数组中的数据是只读的,父组件中的data是可读可写的. 子组件自己的data,子组件可直接访问,与父组件无关. <div class="app"> <mycom1 v-bind:mymsg="msg"></mycom1> <!-- 子组