Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we keep HTML, JS and CSS files separate while creating Vue.js components like in Angular?

Can we keep HTML, JS and CSS files separate while creating Vue.js components?
I've gone through "Why Vue.js doesn't support templateURL" article. The article itself says

"Proper modularization is a necessity if you want to build anything large and maintainable."

However, itself limits the possibility to modularize the code further. I'm coming from an Angular background and I feel separating HTML, JS and CSS is really helpful during development. However the author of the above article has a different opinion. However, this option should have been left up to the developer, so that at least during development he can separate HTML, JS and CSS if he's comfortable doing that.
Apart from modularity, doing the separation will help in being able to reuse these assets anywhere else. All this is lost with the existing opinion.

He also says,

Well, maybe it’s time to up the game a bit and use a proper module bundler like Webpack or Browserify. It might seem daunting if you’ve never dealt with them before, but trust me it’s worth it to take the leap.

But what does it mean? Does it mean that this can be achieved if we use a module bundler like Webpack or Browserify? If yes, how?

Having said all that is there a way to achieve this?

like image 347
Temp O'rary Avatar asked Jan 10 '18 10:01

Temp O'rary


People also ask

Why do we have to separate the js file from HTML?

It separates HTML and code. It makes HTML and JavaScript easier to read and maintain.

Does Vue use HTML and CSS?

Vue (pronounced /vjuː/, like view) is a JavaScript framework for building user interfaces. It builds on top of standard HTML, CSS and JavaScript, and provides a declarative and component-based programming model that helps you efficiently develop user interfaces, be it simple or complex.

Does Vue compile to HTML?

vue-template-loader compiles HTML into individual render functions in the respective TypeScript or JavaScript files.


1 Answers

I found this in the docs but not sure it's what you're looking for

<!-- my-component.vue -->
<template>
<div>This will be pre-compiled</div>
</template>
<script src="./my-component.js"></script>
<style src="./my-component.css"></style>

And doc comment

Even if you don’t like the idea of Single-File Components, you can still leverage its hot-reloading and pre-compilation features by separating your JavaScript and CSS into separate files

like image 87
mistertee Avatar answered Oct 01 '22 19:10

mistertee