简体   繁体   English

使用来自 javascript 的 turbolinks 重定向

[英]Redirect with turbolinks from javascript

是否有可能使window.location.replacewindow.location.href与 turbolinks 一起工作,就像它是一个简单的链接?

From the documentation: https://github.com/rails/turbolinks#triggering-a-turbolinks-visit-manually 从文档: https//github.com/rails/turbolinks#triggering-a-turbolinks-visit-manually

You can use Turbolinks.visit(path) to go to a URL through Turbolinks. 您可以使用Turbolinks.visit(path)通过Turbolinks转到URL。

You can also use redirect_to path, turbolinks: true (or turbolinks: :advance ) in Rails to perform a redirect via Turbolinks. 您还可以在Rails中使用redirect_to path, turbolinks: true (或turbolinks: :advance )来通过Turbolinks执行重定向。


This should work: 这应该工作:

Turbolinks.visit('http://google.com')

Yes. 是。

As written here 正如这里所写

You can use Turbolinks.visit(path) to go to a URL through Turbolinks. 您可以使用Turbolinks.visit(路径)通过Turbolinks转到URL。

In case you implement Turbo Hotwired the basic usage is如果您实施 Turbo Hotwired,基本用法是

Turbo.visit(your_url);

As decribed here如此处所述

other usage with a stimulus controller与刺激控制器一起使用的其他用途

import { Controller } from 'stimulus';
import { visit } from '@hotwired/turbo';

export default class extends Controller {

    myEvent(event){
       // do some code
       // eg <span data-action="click->myController#myEvent" data-url="https://example.com/...">Hello</span> in case 
       // in case element is an button you don't need specify the event (default is click)

       this.goToUrl(event.currentTarget.dataset.url);
    }
  
    goToUrl(url){
       visit(url);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM