首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >wordpress插件中的Angular 6路由器在刷新时不工作

wordpress插件中的Angular 6路由器在刷新时不工作
EN

Stack Overflow用户
提问于 2018-06-10 09:06:09
回答 1查看 353关注 0票数 0

我正在制作一个wordpress插件,它实际上是一个Angular 6 javascript应用程序。我有以下代码,让它在wordpress中作为短代码工作

代码语言:javascript
运行
复制
function msp_helloworld_load(){
    ob_start();
    wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'app/runtime.js' );
    wp_enqueue_script( 'my_custom_script1', plugin_dir_url( __FILE__ ) . 'app/polyfills.js' );
    wp_enqueue_script( 'my_custom_script2', plugin_dir_url( __FILE__ ) . 'app/styles.js' );
    wp_enqueue_script( 'my_custom_script3', plugin_dir_url( __FILE__ ) . 'app/vendor.js' );
    wp_enqueue_script( 'my_custom_script4', plugin_dir_url( __FILE__ ) . 'app/main.js' );    
    echo '<base href="' . $_SERVER[ 'REQUEST_URI' ] . '">';
    echo '<app-root></app-root>';
    echo ob_get_clean();
}
add_shortcode( 'helloworld', 'msp_helloworld_load' );

在这个应用程序中,我使用了一个简单的Angular 6路由器,如下所示

代码语言:javascript
运行
复制
const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'contact',      component: ContactComponent }
];

我将这个简短的代码嵌入到wordpress的示例页面中,如下所示

代码语言:javascript
运行
复制
[helloworld]

这是渲染良好,路由器正在工作,而我在它点击它。这两个urls如下所示

http://localhost/wp/sample-page

http://localhost/wp/sample-page/contact

当我重新加载页面时出现问题。它找不到任何东西。

我的.htaccess看起来像这样

代码语言:javascript
运行
复制
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /hn/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /hn/index.php [N]    
RewriteRule ^ /sample-page/$1 [L]
</IfModule>
# END WordPress

但它仍然不起作用。我不擅长.htaccess,所以请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2018-06-10 22:08:10

太好了,我已经算出来了。这是一个解决方案,首先你不必触摸.htaccess,因为它是默认的wordpress安装

代码语言:javascript
运行
复制
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /hn/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /hn/index.php [L]
</IfModule>
# END WordPress

然后将wordpress固定链接更改为postname

然后这里是诀窍,在基础标签上点你想要嵌入短代码的页面。您可以接受来自用户的参数以指向内容条目中的基本url,或者您甚至可以使其动态放置页面url。就这样。

代码语言:javascript
运行
复制
function msp_helloworld_load(){
    ob_start();
    wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'app/runtime.js' );
    wp_enqueue_script( 'my_custom_script1', plugin_dir_url( __FILE__ ) . 'app/polyfills.js' );
    wp_enqueue_script( 'my_custom_script2', plugin_dir_url( __FILE__ ) . 'app/styles.js' );
    wp_enqueue_script( 'my_custom_script3', plugin_dir_url( __FILE__ ) . 'app/vendor.js' );
    wp_enqueue_script( 'my_custom_script4', plugin_dir_url( __FILE__ ) . 'app/main.js' );    
    echo '<base href="http://localhost/wp/sample-page/">';
    echo '<app-root></app-root>';
    echo ob_get_clean();
}
add_shortcode( 'helloworld', 'msp_helloworld_load' );

还要添加自定义url重写。

代码语言:javascript
运行
复制
function myplugin_rewrite_tag_rule() {  
    add_rewrite_rule( '^sample-page/([^/]*)/?', 'sample-page?city=$matches[1]','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50779784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档