Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Q100】如何使用 react/vue 实现一个 message API #101

Open
shfshanyue opened this issue Dec 5, 2019 · 4 comments
Open

【Q100】如何使用 react/vue 实现一个 message API #101

shfshanyue opened this issue Dec 5, 2019 · 4 comments

Comments

@shfshanyue
Copy link
Owner

可以实现如下 API

message.info()
message.success()

@allan-hx
Copy link

allan-hx commented Dec 28, 2019

import React from 'react';
import ReactDOM from 'react-dom';
// info组件
import Info from 'info';
// success组件
import Success from 'success';

function createMessage(message, Com) {

  let el = document.createElement('div');

  document.body.appendChild(el);

  const component = React.createElement(Com, {
    message
  });

  ReactDOM.render(component, el);
}

const message = {
  info(message) {
    return createMessage(message, Info);
  },
  success(message) {
    return createMessage(message, Success);
  },
};

export default message;

主要实现思路就是创建一个div到body下,然后利用ReactDOM.render将组件渲染到这个容器下,这只是一个简单的实现,没实现关闭和多次调用

@wizzeng
Copy link

wizzeng commented Mar 12, 2020

Vue 实现也是差不多,可以先写好一个 render 函数,作用是把某一HTML片段挂载到 #root 下 / 从 #root 删除该片段。然后写一个 Vue 插件,就是一个暴露了包含 install 方法的模块,install 方法中将 设置 Vue.prototype.$message = message 对象。最后使用 Vue.use 全局注册这个插件即可。

@HuiFeiYa
Copy link

// alert.js 
import Alert from './alert.vue'
import Vue from 'vue'
// 创建构造器
const InstanceAlert = Vue.extend(Alert)

export default class Message {
  deaultOptions = {
    el: document.createElement('div')
    propsData: {
      title: '标题'
    }
  }
  instance = {}
  constructor (options) {
    options = Object.assign({},options,this.deaultOptions)
    this.instance = new InstanceAlert(options)
  }
  show() {
    document.body.appendChild(this.instance.$el)

  }
  hide() {
    document.body.removeChild(this.instance.$el)
  }
}
// alert.vue
<template>
  <div id="alert-mount">
    这里是{{ title }}
  </div>
</template>

<script>
  export default {
    props:{
      title:{
        type:String,
        default:""
      }
    }
  }
</script>

@Drowned-fish
Copy link

用createPortal会好点,符合createPortal的使用场景并且如果message复杂点createElement就用不了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants