Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue 2.0 setting up routes - Do not use 'new' for side effects

I am configuring a vue project. I used the webpack template. ( npm install init webpack ). I am getting an error in the terminal --

ERROR in ./src/main.js

✘  http://eslint.org/docs/rules/no-new  Do not use 'new' for side effects  
/Users/uz067252/Documents/Development/Vue/workex/vue-project/src/main.js:21:1
new Vue({
^


✘ 1 problem (1 error, 0 warnings)


Errors:
1  http://eslint.org/docs/rules/no-new

Here's the main.js

import Vue from 'vue'
import App from './App.vue'
import Hello from './components/Hello.vue'

import VueRouter from 'vue-router'
import VueResource from 'vue-resource'

// We want to apply VueResource and VueRouter
// to our Vue instance
Vue.use(VueResource)
Vue.use(VueRouter)

// Pointing routes to the components they should use
var router = new VueRouter({
routes: [
    { path: '/hello', component: Hello },
    { path: '*', redirect: '/hello' }
]
})

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

Thanks

like image 408
userlkjsflkdsvm Avatar asked Oct 23 '16 23:10

userlkjsflkdsvm


People also ask

Is Vue router 4 compatible with vue2?

It's not possible to use vue-router@4 with vue@2 . They're incompatible with each other.

Is Vue router client side?

Vue Router handles client-side routing in a Vue app. Vue Router is configured with a routes array. Vue Router renders the matched component in the RouterLink component.

Is Vue router necessary?

We need a router when you need to sync URLs to views in your app. It's a very common need, and all the major modern frameworks now allow you to manage routing. The Vue Router library is the way to go for Vue. js applications.


Video Answer


2 Answers

You can also set a vm var and mount separately

var vm = new Vue({router, render: h => h(App)})
vm.$mount('#app')

ref : https://vuejs.org/v2/api/#vm-mount

like image 176
Lucile Fievet Avatar answered Sep 30 '22 21:09

Lucile Fievet


This liner rule is useful and important. If you use any frameworks - for example Vue - that use the new operator side effect, wrap this in an anonymous function by adding three parentheses

(()=>code)();

in your code

(()=>new Vue({  // no linter directive need anymore
    el: '#app',
    router: router,
    render: h => h(App)
}))();
like image 29
sarkiroka Avatar answered Sep 30 '22 19:09

sarkiroka