Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile a ".vue" component in browser, with JS only?

I'd like to compile ".vue" components (with contains html/js/css) into JS, but in browser side, without browserify/vuify/webpack or others ...

In a better world, i'd like to include my ".vue" component into my html app, like that, withoud need of compile things, server side:

<script type="vuejs/component" src="myComp.vue"></script>

It should be possible ?! no ? (And I can't imagine that no one got this idea, or have done it already)

like image 246
manatlan Avatar asked May 10 '17 15:05

manatlan


People also ask

How do I run Vue JS in browser?

Open in browser To view the project, open a tab and type http://localhost:3000 into the URL bar. That's the address that the Node server is listening to. You should now see this page, which is the basic structure of our project.

How do I use Vue component in JavaScript file?

You need to import the vue-multiselect in your main. js file to have access to the component. You also need to install it in your node_modules directory by running npm install vue-multiselect --save first.

Can I use JavaScript in Vue?

In Vue. js, components can be written in HTML, CSS, and JavaScript without dividing them into separate files.


2 Answers

In fact, it's possible with http-vue-loader :

https://github.com/FranckFreiburger/http-vue-loader

like image 196
manatlan Avatar answered Oct 06 '22 01:10

manatlan


It doesn't make sense to compile in the browser when it's so much more efficient to just pre-compile your component locally instead of relying on a visitor's client to do it.

In fact, the answer above regarding vue-http-loader says it's only for use in development and links to this article: https://vuejs.org/2015/10/28/why-no-template-url/

With that said, I created a vue-cli template that lets you pre-compile .vue files into a single .js files you can use in the browser. The single JS file contains the template, script, and styles. It uses webpack, but it's super easy to run and watches your files as you edit them.

$ vue init RonnieSan/vue-browser-sfc my-project

Repo at: https://github.com/RonnieSan/vue-browser-sfc

Instructions are in the README.

like image 21
RonnieSan Avatar answered Oct 06 '22 03:10

RonnieSan