刚学Vue,设置了路由,页面没有显示,是什么原因?

新手上路,请多包涵

设置了路由跳转,组件没有显示(_

网址有变化,但是页面一直都没东西
App.vue

<template>
  <div id="app">
    <nav class="navbar">
      <div class="navbar-brand">
        <img src="/image/c.png" alt="商城Logo" class="logo">
        <h1>在线商城</h1>
      </div>
      <div class="navbar-links">
        <router-link to="/">首页</router-link>
        <router-link to="/all-products">全部商品页</router-link>
        <router-link to="/shopcar">购物车页</router-link>
      </div>
      
    </nav>

    <router-view></router-view>

    <footer class="footer">
      <p>&copy; 2025 在线商城. All rights reserved.</p>
    </footer>
  </div>
</template>


<script>
export default {
  name: 'App'
};
</script>

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

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #333;
  color: white;
  padding: 10px 20px;
}

.navbar-brand {
  display: flex;
  align-items: center;
}

.logo {
  width: 50px;
  height: 50px;
  margin-right: 10px;
}

.navbar-links {
  display: flex;
}

.navbar-links a {
  color: white;
  text-decoration: none;
  margin-left: 20px;
  padding: 5px 10px;
}

.navbar-links a.router-link-exact-active {
  background-color: #42b983;
  border-radius: 5px;
}

.footer {
  background-color: #333;
  color: white;
  text-align: center;
  padding: 10px 0;
  position: fixed;
  bottom: 0;
  width: 100%;
}
</style>

/router/index.js

import { createRouter, createWebHistory } from 'vue-router';
import ShouyeComponent from '../components/ShouyeComponent.vue';
import DianpuA from '../components/DianpuA.vue';
import DianpuB from '../components/DianpuB.vue';
import AllProductsComponent from '../components/AllProductsComponent.vue';
import ProductsComponent from '../components/ProductsComponent.vue';
import ShopcarComponent from '../components/ShopcarComponent.vue';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: ShouyeComponent,
  },
  {
    path: '/dianpu-a',
    name: 'DianpuA',
    component: DianpuA,
  },
  {
    path: '/dianpu-b',
    name: 'DianpuB',
    component: DianpuB,
  },
  {
    path: '/all-products',
    name: 'AllProducts',
    component: AllProductsComponent,
  },
  {
    path: '/product/:id', // 使用动态路由匹配商品详情页
    name: 'Product',
    component: ProductsComponent,
  },
  {
    path: '/shopcar',
    name: 'Shopcar',
    component: ShopcarComponent,
  },
];



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


router.beforeEach((to, from, next) => {

  next();
  
});
export default router;

Main.js

// src/main.js
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

createApp(App)
  .use(router)
  .use(store)
  .mount('#app');

ShouyeComponent.vue

<template>
  <div class="shouye">
    
    <div class="store-buttons">
      <router-link to="/dianpu-a">店铺A</router-link>
      <router-link to="/dianpu-b">店铺B</router-link>
    </div>
    <!-- 路由视图,用于显示路由匹配的组件 -->
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'ShouyeComponent'
}
</script>

<style scoped>
.shouye {
  text-align: center;
  margin-top: 50px;
}
.store-buttons {
  margin-bottom: 20px;
}
.store-buttons button {
  margin: 0 10px;
  padding: 10px 20px;
  font-size: 16px;
}
</style>

AllProductsComponent.vue

<template>
  <div class="all-products">
    <h2>全部商品</h2>
    <ul>
      <li v-for="product in products" :key="product.id">
        <router-link :to="{ name: 'Product', params: { id: product.id } }">
          <img :src="product.image" alt="商品图片" class="product-image">
          <div class="product-info">
            <h3>{{ product.name }}</h3>
            <p>价格: ¥{{ product.price }}</p>
          </div>
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'AllProductsComponent',
  data() {
    return {
      products: [
        { id: 1, name: '商品A1', price: 100, image: '/image/b.jpg' },
        { id: 2, name: '商品A2', price: 200, image: '/image/b.jpg' },
        { id: 3, name: '商品A3', price: 300, image: '/image/b.jpg' },
        { id: 4, name: '商品A4', price: 400, image: '/image/b.jpg' },
        { id: 5, name: '商品A5', price: 500, image: '/image/b.jpg' },
        { id: 6, name: '商品A6', price: 600, image: '/image/b.jpg' },
        { id: 7, name: '商品A7', price: 700, image: '/image/b.jpg' },
        { id: 8, name: '商品A8', price: 800, image: '/image/b.jpg' }
      ]
    };
  }
}
</script>

<style scoped>
.all-products {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
.product-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
}
.product-info {
  text-align: center;
}
</style>

ShopcarComponent.vue

<template>
  <div class="shopcar">
    <h2>购物车</h2>
    <ul>
      <li v-for="item in cartItems" :key="item.id">
        {{ item.name }} - ¥{{ item.price }} x {{ item.quantity }}
        <button @click="decreaseQuantity(item.id)">-</button>
        <button @click="increaseQuantity(item.id)">+</button>
      </li>
    </ul>
    <p>总计: ¥{{ totalPrice }}</p>
  </div>
</template>

<script>
import { useStore } from 'vuex';

export default {
  name: 'ShopcarComponent',
  setup() {
    const store = useStore();
    const cartItems = store.state.cartItems;
    const totalPrice = store.getters.totalPrice;

    const increaseQuantity = (id) => {
      store.dispatch('updateQuantity', { productId: id, quantity: 1 });
    };

    const decreaseQuantity = (id) => {
      const item = store.state.cartItems.find(i => i.id === id);
      if (item.quantity > 1) {
        store.dispatch('updateQuantity', { productId: id, quantity: -1 });
      }
    };

    return { cartItems, totalPrice, increaseQuantity, decreaseQuantity };
  }
}
</script>

<style scoped>
.shopcar {
  text-align: center;
}
</style>

大佬帮我看看呗

阅读 3k
avatarAI BotBETA

回答

问题可能是由于在 ShouyeComponent.vue 中也使用了 <router-view></router-view>,这可能会导致路由视图嵌套,而你的意图可能是只在 App.vue 中显示路由组件。

解决方案

  1. 移除 ShouyeComponent.vue 中的 <router-view></router-view>

    ShouyeComponent.vue 应该只包含它自己的内容,而不是嵌套其他路由组件。

    <template>
      <div class="shouye">
        
        <div class="store-buttons">
          <router-link to="/dianpu-a">店铺A</router-link>
          <router-link to="/dianpu-b">店铺B</router-link>
        </div>
        <!-- 移除这行 -->
        <!-- <router-view></router-view> -->
      </div>
    </template>
  2. 确保其他组件正确无误

    检查 AllProductsComponent.vue, ShopcarComponent.vue 等其他组件是否正确导入并且在路由中正确配置。

  3. 检查控制台错误

    打开浏览器的开发者工具,查看控制台是否有任何错误消息。这些错误可能会提供关于为什么组件没有显示的更多线索。

  4. 确保路由路径正确

    确认你点击的链接路径与路由配置中的路径完全匹配。例如,确保链接到 /all-products 而不是 /AllProducts(虽然大小写在大多数现代浏览器中不是问题,但最好保持一致性)。

  5. 重启开发服务器

    有时候,简单地重启你的开发服务器可以解决缓存或其他奇怪的问题。

应用这些更改后,你的路由应该能够正确显示相应的组件。如果问题仍然存在,请进一步检查控制台中的错误信息。

1 个回答

尝试复制你的代码运行,能正确展示
image.png
查看你的控制台是否有报错

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题