Codificar directamente
Escribe algo en la página
< p> < a href = " #/home" > 首页</ a> |< a href = " #/about" > 关于</ a> |< a href = " #/info" > 信息</ a> </ p>
< div id = " home" >
< h1> 首页</ h1>
</ div>
< div id = " about" >
< h1> 关于</ h1>
</ div>
< div id = " info" >
< h1> 信息</ h1>
</ div>
El primero se muestra por defecto, que es la página de inicio.
< style>
#about {
display : none;
}
#info {
display : none;
}
</ style>
Eche un vistazo al efecto página
Ahora necesita cumplir con los requisitos, haga clic en en La página muestra aproximadamente y al mismo tiempo URL También muestra la etiqueta a correspondiente href Contenido
por hashchange Cambiar eventos para cumplir con este requisito
picadillo ¿Se muestra la URL en la página?
Mucho Si El juicio es dejar que se muestre el texto correspondiente
Document.getElementById no se usa porque se puede usar directamente cuando el elemento es el único elemento en la página
window. onhashchange = function ( e) {
let hash = window. location. hash;
console. log ( hash)
if ( hash == '#/home' ) {
home. style. display = 'block' ;
about. style. display = 'none' ;
info. style. display = 'none' ;
} else if ( hash == '#/about' ) {
home. style. display = 'none' ;
about. style. display = 'block' ;
info. style. display = 'none' ;
} else if ( hash == '#/info' ) {
home. style. display = 'none' ;
about. style. display = 'none' ;
info. style. display = 'block' ;
}
}
Eche un vistazo al impreso picadillo
Eche un vistazo al efecto página
Salto de enrutamiento Vue (juego positivo)
¿Te suena familiar?
Así es como se configura vue para el enrutamiento
camino Se muestra en la barra de direcciones. URL
componente Necesita ser mostrado página , Aquí están esos divs
const router = [
{
path: '#/home' , component: home} ,
{
path: '#/about' , component: about} ,
{
path: '#/info' , component: info}
]
El código js correspondiente
let currentView = router[ 0 ] ;
window. onhashchange = function ( e) {
for ( let i = 0 ; i < router. length; i++ ) {
if ( location. hash == router[ i] . path) {
currentView. component. style. display = 'none' ;
router[ i] . component. style. display = 'block' ;
currentView = router[ i] ;
break ;
}
}
}
Mira si el efecto es el mismo