Coder Social home page Coder Social logo

vue3-tsx's People

Contributors

plum-k avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

zifeiyu-12321

vue3-tsx's Issues

关于子路由下的router-view的问题

需求是:
页面共有一个顶部导航,所以实现了一个layout的组建

// layout.jsx
import { defineComponent } from "vue";
import AppMain from "./AppMain";
import Navbar from "./Navbar";

export default defineComponent({
  name: 'Layout',
  setup(props, ctx) {
    return () => (
      <div>
        <Navbar />
        <AppMain />
      </div>
    )
  }
})

路由设置为

import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "../layout";

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Home',
        component: () => import('../views/home')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
})

router.beforeEach((to, from) => {
  // 返回 false 则取消导航
  if(to.path === '/login') {
    // 登录页面不做限制
    return true
  } else {
    // 判断缓存中是否有 token 如果没有 则跳转到登录页面
    if(localStorage.getItem('token')) {
      return true
    } else {
      router.push('/login')
      return false
    }
  }
})

export default router

关键问题是这里的AppMain中的router-view用jsx怎么写,渲染有问题

import { defineComponent, KeepAlive, Transition } from "vue";
import { RouterView } from "vue-router";

export default defineComponent({
  name: 'AppMain',
  setup(props, ctx) {
    return () => (
      <div class="app-main">
        <p>down is router view</p>
        <RouterView>
          {
            ({Component}) => (
              <KeepAlive>
                <Component />
              </KeepAlive>
            )
          }
        </RouterView>
      </div>
    )
  }
})

router-view中的内容渲染不出来

[Vue warn]: Invalid vnode type when creating vnode: undefined. 
  at <RouterView> 
  at <AppMain> 
  at <Layout onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > 
  at <RouterView> 
  at <App>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.