Vue is a popular JavaScript framework that can be used to build dynamic single-page web applications. The core component of the view layer, Vue.js, can help you build easy-to-maintain applications while also allowing you to better handle user interaction and complex UI components. In most single-page applications, business functions need to be divided into multiple pages for display, and some comparison or analysis of information is also required. At this time, it is very important to use the split-screen function. This article will explain to you how to implement two split screens in a Vue project using the officially recommended method of Vue.
1. Use Vue-Router combined with view components to achieve split screen
First, install Vue-Router (can be installed through npm install vue-router
). Vue-Router is the official routing manager for Vue.js. It is deeply integrated with the core of Vue.js, making it easier to build single-page applications. The following are the steps to use Vue-Router to implement split screen:
Enter the following command on the command line to create a new Vue project:
vue create my-project
It is assumed that you have installed Vue.js according to the official documentation of Vue.js.
In your project, create two new Vue components, namely LeftPane.vue
andRightPane.vue
. These two components will serve as split-screen view components, and each component will showcase a portion of the application UI and functionality.
The sample code can be referred to as follows:
LeftPane.vue:
<template> <div class="left-pane"> <h3>左侧面板</h3> <!-- 添加具体的内容 --> </div> </template> <script> export default { name: 'LeftPane' // 添加其他逻辑 } </script> <style scoped> .left-pane { width: 50%; float: left; } </style>
RightPane.vue:
<template> <div class="right-pane"> <h3>右侧面板</h3> <!-- 添加具体的内容 --> </div> </template> <script> export default { name: 'RightPane' // 添加其他逻辑 } </script> <style scoped> .right-pane { width: 50%; float: right; } </style>
The styles here can be customized according to your actual needs Adjustment.
In your Vue application, create a new folder router
. Create a file index.js
under this folder to define routing configuration. The sample code is as follows:
import Vue from 'vue' import VueRouter from 'vue-router' import LeftPane from '@/components/LeftPane.vue' import RightPane from '@/components/RightPane.vue' Vue.use(VueRouter) const routes = [ { path: '/left', component: LeftPane }, { path: '/right', component: RightPane } ] const router = new VueRouter({ routes }) export default router
Here, you need to introduce LeftPane.vue
and RightPane.vue
into the routing file and define the routing rules. In this way, when the application's routing is switched, Vue-Router will display different split-screen components according to different routing rules.
Find the main.js
file of your project. After importing Vue.js, the code is as follows:
import Vue from 'vue' import App from './App.vue' import router from './router' Vue.config.productionTip = false new Vue({ router, render: h => h(App), }).$mount('#app')
In this code, you will configure the App
component as the Vue root instance and pass router
as a parameter to the component. This allows you to use routing functionality in your application.
The last step is to mount the route view in App.vue
, which will be in your application Implement split screen function. The code example is as follows:
<template> <div id="app"> <router-view></router-view> </div> </template> <script> export default { name: 'App', // 添加其他逻辑 } </script>
The router-view
here will display LeftPane.vue
or RightPane.vue
, depending on the routing rules and user clicks.
2. Use Vue-Router combined with named views to implement split-screen
In addition to the methods introduced above, Vue-Router also supports the use of named views to implement split-screen functionality. This method allows you to display multiple view components at the same time under the same routing rule to achieve a split-screen effect. Here are the steps to implement split screen using named views:
In your App.vue
, create two named views Views, respectively left
and right
. The sample code is as follows:
<template> <div id="app"> <router-view name="left"></router-view> <router-view name="right"></router-view> </div> </template> <script> export default { name: 'App' // 添加其他逻辑 } </script>
The router-view
components here are named left
and right
respectively, and will be loaded in the application at the same time on the main page.
In the routing configuration file, modify the routing rules so that the two components are displayed under the same rule at the same time. The sample code is as follows:
import Vue from 'vue' import VueRouter from 'vue-router' import LeftPane from '@/components/LeftPane.vue' import RightPane from '@/components/RightPane.vue' Vue.use(VueRouter) const routes = [ { path: '/', components: { left: LeftPane, right: RightPane } } ] const router = new VueRouter({ routes }) export default router
Here, the routing rule is modified to path: '/', components:
. Also under this rule, the LeftPane.vue
and RightPane.vue
components are named left
and right
respectively. This way, when the user visits the root path of the application, both components will be displayed on the main page of the application.
Summary
The above is how to apply the split screen function in Vue. Split-screen effect can be achieved by using Vue-Router combined with view components or named views. Choosing different methods to implement the split-screen function in the project according to actual needs will help improve the user experience and ease of use of the application.
The above is the detailed content of How to make two split screens in vue. For more information, please follow other related articles on the PHP Chinese website!