Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How base option works in vue-router

As par documentation of base option:

The base URL of the app. For example, if the entire single page application is served under /app/, then base should use the value "/app/".

But I have tried it like following, It does not seems to work:

const router = new VueRouter({
  base: "/app/",
  routes
})

Demo fiddle.

like image 752
Saurabh Avatar asked Mar 08 '17 06:03

Saurabh


People also ask

How does the Vue router work?

Vue Router helps link between the browser's URL/History and Vue's components allowing for certain paths to render whatever view is associated with it.

How do I pass optional parameters in VUE router?

You can do that by adding a parameter and making it optional. { path: '/users/:id? ', // id is optional components: Users, ... } To make a query parameter optional – you need to add a question mark "?" after the parameter name.

What is router View />?

Router-view vue file. It's basically the view where the components are rendered. It's like the main div that contains all the components, and it returns the component that match the current route.

What is lazy loading in VUE router?

vue file. Lazy loading refers to an approach by which all the scripts are not loaded on the DOM as the application starts. Instead, they're only loaded when requested, which makes the JavaScript bundle size very small at initial load. Vue.


Video Answer


3 Answers

In the Vue Router 4, you set base path by history api:

 const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

Then each path would be prefixed with process.env.BASE_URL

like image 178
Tomek C. Avatar answered Oct 29 '22 05:10

Tomek C.


The base has a default value of '/'. Drawing analogy from how it is used to route:

<router-link to="home">Home</router-link>

or

<router-link :to="{ path: '/abc'}" replace></router-link>

I just omitted the /app and it works. The base doesn't need to be part of the router-link

EDIT

Use of base in vue-router

(For this test I had used vue-cli with the webpack template.)

I had my router configurations like so:

export default new Router({
  base: '/app',
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'RangeInputDemo',
      component: ComponentDemo
    }
  ]
})

Adding base as '/app' made no difference to the routing that happened throughout the project, as if the base was still set to '/'.


I tried to change the url from the server side (the url at which the project is being served).

So in dev-server.js where :

var uri = 'http://localhost:' + port 

controls the url of the app, I made a slight modification to:

var uri = 'http://localhost:' + port + '/app'

This caused the application to show:

enter image description here enter image description here

Notice the fullPath being '/' in the vue console (second image).

Just for double checking, I changed the base to '/' again.

enter image description here


So, the base property of the router configuration is to set the base url as set by the server, if the server serves the application at a route other than '/' then the base can be used for having the application be run from the set url.


Since the question requires the routes being moved under /app, I think having /app as the parent route would be the solution in that case, if the server isn't supposed to change the route on which it serves.

like image 12
Amresh Venugopal Avatar answered Oct 29 '22 03:10

Amresh Venugopal


Had the same problem, setting "base" didn't work - workaround is to update base url in router:

{ name: 'listings', path: baseUrl + '/listings',   component: PageListings}

and refer to routes by name:

<router-link :to="{ name: 'listings' }" exact>Listings</router-link>
like image 4
ego Avatar answered Oct 29 '22 04:10

ego