0
votes

As asked my vue-router doesn't seem to find my component.

The thing I need to say is, that I am adding a new route after a userinput and then I can't access to this particular route. The routes I made static in router.js are still working. I have tried to import the component globally with

import componentName from "./views/componentName"
Vue.component("componentName")

and also tried to import the component only in the router or in the component where I placed the link, which sends us to the component.

I then get following Error message:

[Vue warn]: Failed to mount component: template or render function not defined

Here is my code:

router.js:

import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);
import About from "./views/About.vue";
import Menu from "./views/Menu.vue";
import Home from "./views/Home.vue";
import Test from "./views/Test.vue";
export default new Router({
  routes: [
  {
    path: "/home",
    name: "home",
    component: Home
  },
  {
    path: "/about",
    name: "about",
    component: About
  },
  {
    path: "/menu",
    name: "menu",
    component: Menu
  }
  ]
});

main.js:

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import componentPlugin from "./componentPlugin";
import BootstrapVue from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
Vue.use(componentPlugin);
Vue.use(BootstrapVue);

componentPlugin:

import List from "./components/List.vue";
import MainMenu from "./components/MainMenu.vue";
import Test from "./views/Test.vue";
import About from "./views/About.vue";
import Menu from "./views/Menu.vue";
import Home from "./views/Home.vue";

export default {
  install(Vue) {
    Vue.component("List", List);
    Vue.component("MainMenu", MainMenu);
    Vue.component("Test", Test);
    Vue.component("About", About);
    Vue.component("Menu", Menu);
    Vue.component("Home", Home);
  }
};

List.vue:

<template>
<div>
    <div v-if="title === 'Create new Project'">
        <button @click="nepro = !nepro">{{ title }}</button>
    </div>
    <div v-if="title !== 'Create new Project'">
        <b-container>
            <b-row>
                <b-col>
                    <p>{{ title }}</p>
                    <div id="nav">
                        <router-link :to="'/' + title">{{ title }}</router-link>
                    </div>
                    <router-view></router-view>
                </b-col>
                <b-col><button @click="delProject(id)">X</button></b-col>
            </b-row>
        </b-container>
    </div>
    <div v-if="nepro" @keydown.enter="newProject(projectName)">
        <label for="qwer">Insert Name here: </label>
        <br>
        <input id="qwer" type="text" v-model="projectName">
        <button :disabled="this.$store.state.projectname.length == 0" @click="newProject(projectName)">Finish</button>
    </div>
</div>
</template>

<script>
import { mapGetters } from "vuex";
import Test from "/home/pc-praktikant/VUE/test/src/views/Test.vue";
import About from "/home/pc-praktikant/VUE/test/src/views/About.vue";
export default {
  name: "List",
  components: {
    Test,
    About
  },
  props: {
    title: String,
    id: Number
  },
  data: function() {
    return {
      nepro: false
    };
  },
  watch: {
    reloaded: function() {
      this.newRoute();
    }
  },
  computed: {
    projectName: {
      get() {
        return this.$store.state.projectname;
      },
      set(newVal) {
        this.editProjectName(newVal);
      }
    },
    ...mapGetters({
      reloaded: "getReloaded"
    })
  },
  methods: {
    newRoute() {
      this.$router.addRoutes([
        { path: "/addedRoute", component: { name: "About" } }
      ]);
    },
    newProject(name) {
      this.$store.dispatch("newProject", {
        id: this.$store.getters.getProjectNumber,
        title: this.$store.state.projectname,
        compRate: 0
      });
      this.$store.dispatch("newRoute", {
        path: "/" + name,
        name: name,
        comp: name
      });
      this.$store.commit("newName", "");
      this.nepro = !this.nepro;
    },
    delProject(actualid) {
      this.$store.dispatch("delProject", actualid);
    },
    editProjectName(name) {
      this.$store.commit("newName", name);
    }
  }
};
</script>

<style>
</style>

With addRoutes I am adding a route and this route is my problem.

App.vue:

<template>
<div id="app">
    <div id="nav">
        <router-link to="/home">Home</router-link> |
        <router-link to="/about">About</router-link> |
        <router-link to="/menu">Menu</router-link>
    </div>
    <router-view/>
</div>
</template>

 <script>
export default {
  name: "App",
  Components: {},
  data: function() {
    return {};
  },
  watch: {},
  computed: {},
  methods: {}
};
</script>

<style lang="scss">
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;
  a {
    font-weight: bold;
    color: #2c3e50;
    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>

Here the link to /menu goes to a component who calls the List.vue for every project in my vuex store

store.js:

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    projects: [{ id: 0, title: "Create new Project", compRate: 0 }],
    globalid: 1,
    projectname: "",
    reloaded: 0
  },
  mutations: {
    newProject: (state, project) => {
      state.projects.push({
        id: project.id,
        title: project.title,
        compRate: project.compRate
      });
    },
    delProject: (state, id) => {
      state.projects.forEach(e => {
        if (id === e.id) {
          state.projects.splice(state.projects.indexOf(e), 1);
        }
      });
    },
    newName: (state, name) => {
      state.projectname = name;
    },
    newRoute: state => {
      state.reloaded++;
    }
  },
  actions: {
    newProject: ({ commit, state }, project) => {
      commit("newProject", {
        id: state.globalid,
        title: project.title,
        compRate: project.compRate
      });
      state.globalid++;
    },
    delProject: ({ commit }, id) => {
      commit("delProject", id);
    },
    newRoute: ({ commit }) => {
      commit("newRoute");
    }
  },
  getters: {
    getProjectNumber(state) {
      return state.projects.length;
    },
    getReloaded(state) {
      return state.reloaded;
    }
  }
});

I am using Vue v2.5.17 and vue-router 2.0.

If you need any more Information let me know.

1

1 Answers

1
votes

You can't define a component globally like this. You forgot to declare the contents and functions of this component when declaring a component globally. This problem has been pointed out in the error report.

A correct definition code should be like this

import componentName from "./views/componentName"
Vue.component("componentName", componentName)

You can find and define more of the components and corpus definitions in the official manual.

https://vuejs.org/v2/guide/components-registration.html#Component-Names

Hope it helps you