Unable to access component template during routing操作

Currently implementing Vuejs. Here is the main js code snippet:

import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false;


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

This is the router setup:

import Vue from 'vue';
import VueRouter from 'vue-router';
import ControlExcel from './components/ControlExcel';
import Login from "./components/Login";
import blog from './components/blog';
import file from './components/file';


Vue.use(VueRouter);

let routes = [
    {
        path: '/control-excel',
        name: 'ControlExcel',
        component: ControlExcel
    }
    ,
    {
        path: '/loginn',
        component: Login
    }
    ,
    {
        path: '/blog',
        name: 'blog',
        component: blog
    }
    ,
    {
        path: '/file',
        name: 'file',
        component: file
    },


];

export default new VueRouter({
    mode: 'history',
    base: "",
    routes
})

Here's a snippet from the app vue file:

<template>
  <div id="app">
<!--    <img alt="Vue logo" src="./assets/logo.png">-->
<!--    <HelloWorld msg="Welcome to Your Vue.js App"/>-->

<!--    <ControlExcel />-->

<!--      <component v-bind:is="component" />-->
<span>abcd</span>




      <li class="nav-item">
          <router-link class="nav-link" to="/control-excel">exce</router-link>
      </li>
      <li class="nav-item">
          <router-link class="nav-link" to="/loginn">loginn</router-link>
      </li>

      <li class="nav-item">
          <router-link class="nav-link" to="/blog">blog</router-link>
      </li>

      <li class="nav-item">
          <router-link class="nav-link" to="/file">file</router-link>
      </li>

  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import Login from './components/Login.vue'
// import ControlExcel from "./components/ControlExcel";
// import Login from "./components/Login";
import file from './components/file';
import Vue from 'vue';

Vue.component('file',file);

// import DownloadExcel from './components/DownloadExcel'

export default {
  name: 'app',
  components: {
      // Login,
      // ControlExcel,

  }
  ,
    data() {
        return {
            // component: "Login"
        }
    }
}
</script>

On localhost:8080, the following are displayed:

abcd
exce
loginn
blog
file

Even though the URL changes when clicked on any of them (e.g., http://localhost:8080/file for file), the component doesn't load.

For instance, for file:

  {
        path: '/file',
        name: 'file',
        component: file
    },

The content of the file.vue component is as follows:

<template>
    <div class="blog">
        <h1>fasfsasd</h1>
    </div>
</template>
<script>
    export default{
        name:'file'
        }

</script>
<style scoped>

</style>

Current directory structure:

src 
-components
--blog.vue
--ControlExcel.vue
--file.vue
--Login.vue
router.js
main.js
App.vue

Question arises as to why the template isn't loading along with the component?

There is another way using dynamic components, but it might not be considered best practice. For reference, see: https://blog.logrocket.com/how-to-make-your-components-dynamic-in-vue-js/

Answer №1

With Vue-Router, the main component remains unchanged while only displaying the component that matches the URL within

<router-view></router-view>
, which seems to be missing in your setup. Make sure to include it in your main component.

One way to do this is:

<div id="app">
  <span>abcd</span>
  <ul>
      <li class="nav-item">
          <router-link class="nav-link" to="/control-excel">exce</router-link>
      </li>
      <li class="nav-item">
          <router-link class="nav-link" to="/loginn">loginn</router-link>
      </li>

      <li class="nav-item">
          <router-link class="nav-link" to="/blog">blog</router-link>
      </li>

      <li class="nav-item">
          <router-link class="nav-link" to="/file">file</router-link>
      </li>
  </ul>

  <router-view></router-view>

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

working with received data from a JavaScript object

Looking for code assistance, I have a data object serving as my data source. var data = [ { "label":"May 7", "value":25736.6, "proID":"ISB" }, // more data objects... ]; I'm manipulating this data to generate another ...

What is the best approach for Angular directives: utilizing a single object or separate values as attributes?

This question pertains to best practices, but I do believe there is a definitive answer. I have a directive that offers six customizable options. Should I assign each option to a separate attribute on the directive (as shown below): <my-directive my ...

Discover the Names of Elements associated with the identical Input File tag

Having a bit of trouble with my jQuery code here. I've got two HTML input file elements with different names. How can I retrieve the name of the second input file using jQuery? Below is my HTML markup: <input type="file" name="photo_a" /> &l ...

Encountering a 500 status error while trying to deploy a Vue web application using Vite and Nginx within a Docker Compose

I am currently honing my skills in web development and working on a project utilizing the following technology stack: Frontend: Vue Build tool: Vite Deployment: Nginx (locally) Although I have successfully deployed the app locally, when attempting to acc ...

Tips for resolving the error "Parsing near 'Unexpected end of JSON input while parsing...' for...'mocha':'^3.2.0','s'":

Once I've successfully set up react and react-dom, the next step is to install webpack. However, I encountered an error during this process. To troubleshoot, I decided to install babel-loader first to ensure that both npm and my internet connection a ...

Can someone explain how to use JavaScript to make table data fill the entire row in a table?

After clicking the button, the layout of the table changes. I want to keep the original table layout even after the JavaScript function runs. I am still learning about JavaScript. function toggle(button) { if(document.getElementById("1").value=="Show ...

Delay the rendering of the MUI DataGrid column until after the DataGrid is visible on the screen

Would it be feasible to asynchronously load a column of data in the MUI DataGrid after the table is displayed on the screen? Retrieving this additional data from the database is time-consuming, so I aim to have it appear once the table has fully loaded. T ...

encountering an issue with the react hook useHistory leading to an error

I recently encountered an issue while implementing useHistory() in my code. Previously, I had used it without any errors, but now I'm getting the following error in this component: Line 6:18: React Hook "useHistory" is called in function "showPost" ...

Calculating the total price of items in a shopping cart by multiplying them with the quantity in Vue.js

I am currently working on enhancing the cart system in Vue.js, with a focus on displaying the total sum of product prices calculated by multiplying the price with the quantity. In my previous experience working with PHP, I achieved this calculation using ...

Tips for saving an image file using the source I've generated from my drawing on an HTML 5 canvas

Recently, I utilized HTML 5 canvas to draw an image using JavaScript. After completing the drawing, I needed to save the canvas on my hard disk. To accomplish this, I followed a method to obtain the image source: var img = canvas.toDataURL(); Subsequentl ...

Converting JSON to CSV: Simplifying the process of generating a table column for every field in a collection with Papa.unparse()

Using Papa Parse 4, I am encountering an issue when using Papa.unparse(collection). It appears that the resulting table is only generating columns based on the fields of the first document in my JSON collection. I would like all possible fields from my col ...

Converting R functions into JavaScript using Emscripten

Attempting to convert R functions written in C to JavaScript using Emscripten is proving to be a challenging task. The primary objective at this stage is to migrate a function called pf. The source code for the function can be accessed here. As I navigate ...

How can I limit the keys in a Typescript object to only certain strings?

Is there a way in Typescript to create an object of type Partial with keys that can only be a combination of 'a', 'b', or 'c'? The object should not have all 3 keys, but it must have at least one. Here's what I've at ...

Retrieve the 'current' scope using a 'master' controller

I have a main controller that I refer to as my 'root' controller, which is declared using the ng-controller attribute. Additionally, I have a few other controllers that are created dynamically through a $routeProvider. I want to define a function ...

Customizing compilation parameters in Webpack.mix for Vue.js applications

My webpack.mix.js file (laravel mix) successfully compiles javascript and scss, but I need to make a parameter dynamic. Currently, my code looks like this: //let result = 'https://www.web1.com/'; let result = 'https://stage.web1.com/'; ...

The error in React syntax doesn't appear to be visible in CodePen

Currently, I am diving into the React Tutorial for creating a tic-tac-toe game. If you'd like to take a look at my code on Codepen, feel free to click here. Upon reviewing my code, I noticed a bug at line 21 where there is a missing comma after ' ...

Is there a way to turn off vue.js transitions specifically for testing purposes?

I'm utilizing a vue.js component with the <transition> element for show/hide animations. However, I want to disable the animation for faster testing. How can I achieve this? The solution proposed is * { transition: none !important } in this lin ...

Generate a revised object from a function and return it to the caller

What is the most efficient way to update and return a new object from a function? I came up with this function: export const addItemToCart = (currentCart, item) => { const { name, ...otherProps } = item; // if the item is already in the cart if ...

NodeJS application does not acknowledge the term "Require"

I have completed the steps outlined on http://expressjs.com/en/starter/installing.html to set up my express NodeJS application. Following the installation, we navigated to the "myapp" directory and installed the "aws-iot-device-sdk" from https://github.com ...

Vue infinite loop occurring in a method

I keep receiving this warning [Vue warn]: You may have an infinite update loop in a component render function. and I believe I have pinpointed the reason behind it. My Scenario : I am passing an object with an index to a Vue method within a loop. Computed ...