Coder Social home page Coder Social logo

remote-vue2-loader's Introduction

remote-vue2-loader

描述

在vue2 单页面中加载远程组件,兼容scss,支持浏览器端es6语法降级

修改自 http-vue-loader,解决了浏览器端es6语法降级问题、scss样式问题、以及export default {} 语法问题

安装

npm install remote-vue2-loader

使用

<template>
  <component :is="remoteComponent"></component>
</template>

<script>
import remoteVueLoader from 'remote-vue2-loader'
export default {
  data () {
    return {
      remoteComponent: null
    }
  },
  created () {
    this.getRemoteComponent()
  },
  methods: {
    // 尝试渲染远程文件或远程字符串
    async getRemoteComponent() {
      // 从远程服务器加载组件
      this.remoteComponent = remoteVueLoader('127.0.0.1/testA.vue')
    }
  }
}
</script>

说明

上述远程的testA.vue文件内容可能如下:

<template>
  <component :is="remoteComponent"></component>
</template>

<script>
import remoteVueLoader from 'remote-vue2-loader'
export default {
  data () {
    return {
      remoteComponent: null
    }
  },
  created () {
    this.getRemoteComponent()
  },
  methods: {
    // 尝试渲染远程文件或远程字符串
    async getRemoteComponent() {
      // 情况1. 直接从从远程服务器加载组件
      this.remoteComponent = remoteVueLoader('127.0.0.1/testA.vue')
      
      // 情况2. 如果接口是post等其他类型接口,或者需要添加参数,也可以这么写
      axios.get('http://127.0.0.1/testA.vue', {
        params: {
          p1: 'xxx',
          p2: 'xxx'
        }
      }).then(res => {
        this.remoteComponent = remoteVueLoader('data:text/plain,' + encodeURIComponent(res.data))
      })
    }
  }
}

remote-vue2-loader's People

Contributors

hunterxing avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.