Note: Can we write vue.js large application without using any compiler for code like currently i see all example use webpack now to make vue.js code compatible for browser .
I want make vue.js
application without webpack
and without using .vue
extension. Is it possible? if it is possible, can you provide a link or give sample how to use routing in that case.
As we make component in .vue
extension can be make component in .js
extension and use application as we do in angular 1 where we can make whole app without any trans-compiler to convert the code.
Can be done that in html , css , js file only and no webpack sort of thing.
What i have done . index.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>vueapp01</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
main.js this file added in webpack load time
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
App.vue
<template>
<div id="app">
<img src="./assets/logo.png">
<a href="#/hello">Hello route</a>
<a href="#/">Helloworld route</a>
{{route}}
<router-view/>
<!-- <hello></hello> -->
</div>
</template>
<script>
export default {
name: 'App',
data () {
return {
route : "This is main page"
}
}
}
</script>
router
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Hello from '../components/Hello'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/hello',
name: 'Hello',
component: Hello
}
]
})
I have done something like this . Can we do this by just html , css , js file only with not webpack to compile code . Like we do in angular 1 .
Thanks
As stated in this jsFiddle: http://jsfiddle.net/posva/wtpuevc6/ , you have no obligation to use webpack or .vue files.
The code below is not from me and all credit goes to this jsFiddle creator:
Create an index.html file:
Home.js
Foo.js
router.js
index.js
Appreciate the framework...
Just a sidenote:
.vue
files are really awesome, you should definitely try them if not using them is not a requirementFor sure you can. We did a project with Vue, and we had couple of problems during compiling
.vue
files. So we switched to structure with three separate files.But be aware that you need
webpack
anyway. The idea of Vue was to split huge projects into components, so using template inside.js
file it's pretty normal. So take a look athtml-loader And css-loader
Using these modules you can write something like this:
component.js
component.css
component.html
BUT
You need to know that HTML file should be written in the same way as I you will have it in
template
property.Also, take a look at this repo, maybe you will find something useful here
Vue-html-loader. It is a fork from
html-loader
by Vue core team.You perfectly can, but with a lot of disadvantages. For example: you cannot easily use any preprocessor, like Sass or Less; or TypeScript or transpile source code with Babel.
If you dont need support for older browser, you can use ES6 modules today. Almost all browsers support it. See: ES6-Module. But Firefox dont support dynamyc import(). Only Firefox 66 (Nightly) support it and need to be enabled. And if that wasn't enough, your web application will not be indexed. It's bad for SEO. For example, Googlebot can craw and index Javascript code but still uses older Chrome 41 for rendering, and it's version don't support ES6 modules.
If that are not disadvantages for you, then you can do this:
1 - Remove any thirty party library import like Vue, VueRouter, etc. And include those in the index.html file using script tags. All global variables are accesible in all es6 modules. For example, remove this line from main.js and all .vue files:
And add this line in your index.html:
2 - Rewrite all .vue files and change file extension to .js. For example, rewrite something like this:
To something like this:
3 - It is all. I put an example in this link
P.S. Text editing without syntax highlighting can be frustrating. If you use Visual Studio Code you can install Template Literal Editor extension. It allows editing literal strings with syntax highlight. For styles select CSS syntax, and for templates HTML syntax. Unknown tag in HTML are highlighted diferently. For solve this, change the color theme. For example, install Brackets Dark Pro color theme or any theme do you like.
Regards!
I have started learning vue.js also and I am not familiar with webpack and stuff and I also wanted to still separate and use .vue files as it makes management and code cleaner.
I have found this library: https://github.com/FranckFreiburger/http-vue-loader
and a sample project using it: https://github.com/kafkaca/vue-without-webpack
I am using it and it seems to work fine.