Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I generate a component in the following way? #9

Closed
ConardLi opened this issue Aug 4, 2018 · 10 comments
Closed

How can I generate a component in the following way? #9

ConardLi opened this issue Aug 4, 2018 · 10 comments

Comments

@ConardLi
Copy link

ConardLi commented Aug 4, 2018

How can I generate a component in the following way?

var MyComponent = Vue.extend({ template: '<text>This is a component</text>' }); Vue.component('my-component', MyComponent);

@ConardLi
Copy link
Author

ConardLi commented Aug 4, 2018

I got a string like this from the server and then dynamically created the component.
function factory () {\n return {\n template: '<text> hello </text>'\n}

@SmallComfort
Copy link
Owner

SmallComfort commented Aug 5, 2018

嗨~ 你是国内的啊!
react-vue 在运行的时候会启动一个编译器,只会编译 <template/> 标签内的模板,js 内的模板字符串是无法编译的。
你的模板代码需要写成常规的 vue 组件。

@ConardLi
Copy link
Author

ConardLi commented Aug 5, 2018

现在的需求是我要从服务器获取一段vue的脚本(在web环境下我可以用Vue.component方法生成组件),在rn环境下使用你的Vue.component是没办法生成组件的,请问有什么好的解决办法?

@SmallComfort
Copy link
Owner

不支持动态执行

@ConardLi
Copy link
Author

ConardLi commented Aug 5, 2018

没有办法把js中的模版代码加到页面中的<template/>中么?

@SmallComfort
Copy link
Owner

你跑的是 react 环境,如果想要动态执行的话,用 react 的动态方案,react-vue 默认只会编译 .vue 文件,.js 和 .jsx 里面可以写 react,相互引入没问题的。

@ConardLi
Copy link
Author

ConardLi commented Aug 5, 2018

谢了,我试一下。

@ConardLi
Copy link
Author

ConardLi commented Aug 5, 2018

          var Test = React.createClass({
            render: function() {
              return <text>Type something:</text>;
            }
          });
          this.component = Vue.component("component", Test);

不可以这样写吗?
报错 Expected a component class,got [object,Object].

@SmallComfort
Copy link
Owner

react 组件和 vue 组件分开写,看看这个 demo 是怎么引入的 https://github.com/SmallComfort/react-vue-demo/blob/master/src/VueDemo/Counter.vue

@ConardLi
Copy link
Author

ConardLi commented Aug 5, 2018

想了想好像没办法执行。。因为从服务器拿到的是vue的语法,生成react的组件好像不太现实

@ConardLi ConardLi closed this as completed Aug 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants