简体   繁体   English

如何从电子应用程序内部构建Vue视图以安装为插件

[英]How to build vue views from within electron apps to mount as plugins

I am writing a reporting app that runs in electron. 我正在编写一个以电子方式运行的报告应用程序。 Reporting means I have a data source and users write views that arrange the data in whichever ways they like so it can be printed out or archived as PDF. 报告意味着我有一个数据源,用户可以编写视图以任意方式排列数据,以便可以将其打印或存档为PDF。

Let's say there is a list of student data along with grades for courses. 假设有一个学生数据列表以及课程成绩。 The reporing app allows you to create a report that has a data table with names and grades for each course. 报表应用程序允许您创建一个包含数据表的报告,该数据表包含每个课程的名称和成绩。 So you end up with grade reports. 因此,您最终得到成绩报告。 Someone else wants just names and addresses another one wants to create a library card for each student. 有人只想说出姓名和地址,另一个人想为每个学生创建图书馆证。 HTML and CSS combined with Vue make a great foundation to write this stuff. HTML和CSS与Vue的结合为编写此书奠定了良好的基础。

The easy part is to fetch data from the database and offer json as a data source to views. 最简单的部分是从数据库中获取数据,并将json作为数据源提供给视图。 The difficult part is how to let users add their reports to the app. 困难的部分是如何让用户将他们的报告添加到应用程序。 In dev mode I can just add views to directories, import them and render the data as part of the app. 在开发模式下,我可以将视图添加到目录中,将其导入并作为应用程序的一部分呈现数据。 But I have my views available at build time. 但是我在构建时可以发表自己的观点。 Once I have built and shipped the production app it's out of my hands and the user can't add their views to the app anymore because they don't rebuild the app. 一旦我构建并交付了生产应用程序后,它就不可用了,用户无法再将其视图添加到该应用程序中,因为他们不重建应用程序。 They just want to point to a directory, add their views and use them. 他们只想指向目录,添加视图并使用它们。

I have decided to use single file components because they allow users to simply add calculations and maybe use pug etc to keep it simple. 我决定使用单个文件组件,因为它们允许用户简单地添加计算,并可能使用pug等保持简单。 Besides I can store data about the component in a straight forward way (like name of report, department etc) 此外,我可以直接存储有关组件的数据(如报告名称,部门名称等)

To my knowledge I need to build those views programmatically and load them dynamically. 据我所知,我需要以编程方式构建这些视图并动态加载它们。

So my questions are: 所以我的问题是:

  1. How can I build and mount single file components (.vue) dynamically from within a production app that has no prior knowledge of user produced files? 如何在不具有用户生成文件先验知识的生产应用程序中动态构建和安装单个文件组件(.vue)?
  2. Do I need to build these components or is there a way to utilize on board tools to just read and render these components? 我是否需要构建这些组件,或者是否可以利用板上工具来读取和渲染这些组件?
  3. Is there a better way to serve these files? 有没有更好的方法来提供这些文件?

More information: Security is not a concern. 详细信息:安全性不是问题。 So loading user created views is ok. 因此,加载用户创建的视图是可以的。 It needs to be working in electron. 它需要在电子中工作。 I do have a server based app written in Ruby that does the same thing but the server is going away anytime soon so it has to be client based and executable. 我确实有一个用Ruby编写的基于服务器的应用程序,该应用程序执行相同的操作,但是该服务器很快就会消失,因此它必须基于客户端并且可执行。 I cannot install node on client PCs so they could just build their own app and include their own views via hot reloading. 我无法在客户端PC上安装节点,因此它们只能构建自己的应用程序并通过热重载来包含自己的视图。

If you're interested in reporting apps there is one in JS but server based: https://jsreport.net/ or mine, ruby based: https://github.com/hmt/sahib 如果您对报告应用程序感兴趣,那么JS中有一个,但是基于服务器的: https : //jsreport.net/或我的基于ruby的应用程序: https : //github.com/hmt/sahib

You could watch the directory and respond to new files being add. 您可以查看目录并响应正在添加的新文件。 That's for the dynamic part of your question. 那是您问题的动态部分。 A nice way is node-fs . 一个不错的方法是node-fs

Using the same lib you can serialize and deserialize object to or from file, letting you load the file from your user. 使用相同的库,您可以将对象与文件进行序列化和反序列化,从而使您可以从用户中加载文件。

At this point, you only need to be able to change back and forth from an object to a Vue component. 此时,您只需要能够从一个对象到一个Vue组件来回切换。 As far As I know, this is no problem as you can 据我所知,这没问题

new Vue (object)

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

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