# 我们可以用 \`.gjs\` 作为路由模板吗？

**URL:** https://meta.discourse.org/t/can-we-use-gjs-for-route-templates/357046
**Category:** Development
**Created:** [2025年三月12日 23:39 UTC](https://meta.discourse.org/t/can-we-use-gjs-for-route-templates/357046 "2025-03-12T23:39:40Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![bitmage](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bitmage/32/389881_2.png) [@bitmage](https://meta.discourse.org/u/bitmage)
#### Post date: [2025年三月13日 04:10 UTC](https://meta.discourse.org/t/can-we-use-gjs-for-route-templates/357046/4 "2025-03-13T04:10:46Z")

</div>

在应用程序中添加新路由非常不直观。我将在此处记录下来供他人参考。

我想在根目录下添加一个 `/print` 路由。

- 本教程记录了[如何添加 `/admin` 路由](https://meta.discourse.org/t/creating-routes-in-discourse-and-showing-data/48827)，但没有记录普通路由。
- 这里有关于[如何在根路径添加 API 端点](https://meta.discourse.org/t/how-to-add-route-to-root-path/186141)的信息（不是 Ember 路由）。
- 这里有一个[关于排查顶级 Ember 路由的冗长讨论](https://meta.discourse.org/t/trouble-getting-a-top-level-page-to-ember/56007)。

我最终使用的代码如下。`county-fence` 是我的插件名称，请将其替换为您自己的名称。

### Ember 路由

assets/javascripts/discourse/routes/county-fence-route-map.js.es6

```js
export default function() {
  this.route('print', { path: '/print' });
}

```

assets/javascripts/discourse/routes/print.js

```js
import Route from "@ember/routing/route";

export default class PrintRoute extends Route {
  model() {
    return { message: "This is a custom print page!" };
  }
}

```

assets/javascripts/discourse/templates/print.gjs

```js
import RouteTemplate from "ember-route-template";
import ...;

export default RouteTemplate(
  <template>
    ...
  </template>
)

```

### API 路由

plugin.rb

```rb
after_initialize do
  require_relative "app/controllers/print_controller"
end

```

app/controllers/print\_controller.rb

```rb
# frozen_string_literal: true
# HTTP Status codes: https://github.com/discourse/discourse/blob/main/lib/discourse.rb

class ::CountyFence::PrintController < ::ApplicationController
  requires_plugin CountyFence::PLUGIN_NAME

  def save_print
  end

  def list_prints
    render json: { name: "donut", description: "delicious!" }
  end

  def get_print
  end

end

Discourse::Application.routes.append do
  post "/print" => "county_fence/print#save_print"
  get "/print" => "county_fence/print#list_prints"
  get "/print/:id" => "county_fence/print#get_print"
end

```

---

_[View the full topic](https://meta.discourse.org/t/can-we-use-gjs-for-route-templates/357046)._
