用vue创建手机端和PC端两个页面 通过屏幕适配页面

1.创建 Vue 项目:

npm install -g @vue/cli
vue create my-vue-project
cd my-vue-project

2.创建手机端页面和 PC 端页面:

在 src 目录下创建两个不同的目录,用于存放手机端和 PC 端页面。

cd src
mkdir mobile pc

在 mobile 目录下创建一个手机端页面组件,比如 MobilePage.vue,在 pc 目录下创建一个 PC 端页面组件,比如 PCPage.vue。

<!-- src/mobile/MobilePage.vue -->
<template>
  <div>
    <h1>手机端页面</h1>
    <!-- 手机端页面内容 -->
  </div>
</template>
 
<script>
export default {
  // 页面逻辑
}
</script>
 
<style>
/* 手机端页面的样式 */
</style>

<!-- src/pc/PCPage.vue -->
<template>
  <div>
    <h1>PC端页面</h1>
    <!-- PC端页面内容 -->
  </div>
</template>
 
<script>
export default {
  // 页面逻辑
}
</script>
 
<style>
/* PC端页面的样式 */
</style>

3.配置路由:

在 src 目录下创建一个名为 router.js 的文件,用于配置路由。根据不同设备的屏幕大小,我们将选择加载不同的页面组件

先把路由 加进来

npm install vue-router --save
// src/router.js
 
import { createRouter, createWebHistory } from 'vue-router';
import MobilePage from './mobile/MobilePage.vue';
import PCPage from './pc/PCPage.vue';
 
const routes = [
  {
    path: '/',
    component: window.innerWidth < 768 ? MobilePage : PCPage
  }
];
 
const router = createRouter({
  history: createWebHistory(),
  routes
});
 
export default router;

4.修改 main.js:

在 src 目录下的 main.js 文件中,引入 router.js 并配置路由。


// src/main.js
 
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
 
createApp(App).use(router).mount('#app');

5.修改 App.vue:

在 src 目录下的 App.vue 文件中,我们需要根据不同设备的屏幕大小来加载对应的页面组件。

<!-- src/App.vue -->
<template>
  <router-view />
</template>
 
<style>
/* PC 端样式 */
#app {
  max-width: 1024px;
  margin: 0 auto;
}
 
/* 手机端样式 */
@media (max-width: 767px) {
  #app {
    max-width: 100%;
    padding: 0 10px;
  }
}
</style>

6.运行项目

 npm run serve
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值