简体   繁体   English

vuejs-如何将数据传递到Webpacked根实例

[英]vuejs - how to pass data to webpacked root instance

I have an application where I'm using Single File Components for showing data in a table. 我有一个应用程序,其中使用Single File Components在表中显示数据。 The data is queried based on a search string and columns that I want to include in the results (the data is fetched by basically an ajax request). 基于search字符串和我要包括在结果中的列来查询数据(数据基本上是通过ajax请求获取的)。

My goal is to create a reusable component so that I can pass the query text and the column names to the component. 我的目标是创建一个可重用的组件,以便可以将查询文本和列名传递给该组件。 Normally I could use props and slots for this, but since I'm using Single File Components I have no idea how to pass data to the component. 通常,我可以为此使用propsslots ,但是由于我使用的是Single File Components所以我不知道如何将数据传递到组件。

The " public " interface to the Single File Component looks as follows: Single File Component的“ public ”接口如下所示:

<!-- index.hmtl -->
<div id="app"></div>
<script src="./build/webpackedComponent.js"></script>

// main.js / Component entry point
import Vue from 'vue'
import App from './components/app.vue'

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

Inside App I need the data for the ajax request. App内部,我需要ajax请求的数据。 An idea I had was to import a json file in the main entry point of the component, but then I would have to repack the component everytime I change the json, and for multiple uses of the component, I need to pack them each time for every instance that uses different data... 我的想法是在组件的主入口处导入json文件,但是每次更改json时,我都必须重新打包组件,并且对于组件的多种用途,我每次都需要打包它们每个使用不同数据的实例...

Any ideas? 有任何想法吗?

Find your webpack.base.conf.js file (in my project it is located inside the build folder) or the equivalent configuration file, then add library: 'myLibrary' to the output object. 找到您的webpack.base.conf.js文件(在我的项目中,它位于build文件夹内)或等效的配置文件,然后将library: 'myLibrary'添加到输出对象。

(should end up with something like this:) (最终应该是这样的:)

module.exports = {
  entry: {
    app: ['./src/main.js']
  },
  output: {
    library: 'myLibrary',
    path: config.build.assetsRoot,
    publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
    filename: '[name].js'
  },
  .
  .
  .

then in main.js, you can export Vue like this: 然后在main.js中,您可以像这样导出Vue:

// main.js / Component entry point
import Vue from 'vue'
import App from './components/app.vue'

export { Vue, App };

And now in the global scope you can do this: 现在,您可以在全球范围内执行以下操作:

new myLibrary.Vue({
  el: '#app',
  data: {
    value1: 12,
    value2: 14
  },
  components: { App: myLibrary.App },
  template: '<App :prop1="value1", :prop2="value2"></App>'
});

That is, whatever you export in main.js will be accessible to the global scope via the name of the library you provided in your webpack config file... in this case, myLibrary . 也就是说,无论您在main.js中导出的​​内容如何,​​都可以通过您在webpack配置文件中提供的库的名称在全局范围内访问...在本例中为myLibrary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM