深入了解React(十五、路由1)

/**
 * 页面路由
 * @type {string}
 */
// 跳转
window.location.href = 'https://www.baidu.com';
// 回退
history.back();


/**
 * hash 路由
 * @type {string}
 */
// 跳转【也可以用a链接】
window.location = '#hash';
// 事件:onhashchange
window.onhashchange = function () {
    console.log('current hash:', window.location.hash);
};

/**
 * h5路由
 * @type {string}
 */
// 推进一个状态
history.pushState('name', 'title', '/path');
// 替换一个状态
history.replaceState('name', 'title', '/path');
// 事件:popstate
window.onpopstate = function () {
    // 全路径
    console.log(window.location.href);
    // 绝对路径
    console.log(window.location.pathname);
    // 哈希
    console.log(window.location.hash);
    // 
    console.log(window.location.search);
};
发布了123 篇原创文章 · 获赞 4 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/yuzhiboyouzhu/article/details/80032960
今日推荐