在插件中,怎么自定义路由,然后渲染自定义页面?
我看这个帖子:开发 Discourse 插件 - 第五部分 - 添加管理员界面 - 文档 / 开发者指南 - Discourse Meta,以及其他插件,但是都失败了。
自定义的路由都返回json数据了,而我想要的是渲染ember的模板。这里有哪篇帖子是解释相关知识的吗?多谢多谢。
在插件中,怎么自定义路由,然后渲染自定义页面?
我看这个帖子:开发 Discourse 插件 - 第五部分 - 添加管理员界面 - 文档 / 开发者指南 - Discourse Meta,以及其他插件,但是都失败了。
自定义的路由都返回json数据了,而我想要的是渲染ember的模板。这里有哪篇帖子是解释相关知识的吗?多谢多谢。
如果您想做的就是这样,请查看:
以及:
我目前是卡在这里了,下面是我的具体步骤:
我在plugins/q/config/routes.rb文件里面定义了路由:
get “/hello” => “examples#hello”
控制器:plugins/q/app/controllers/quectel/examples_controller.rb
def hello
render json: { hello: "world" }
end
现在访问:localhost:4200/q/hello,返回了json
{"hello":"world"}
我怎么才能在访问localhost:4200/q/hello的时候,渲染ember的模板呢?
我以前收藏了这个主题以便稍后阅读:https://meta.discourse.org/t/creating-routes-in-discourse-and-showing-data/48827。不确定它是否是最新的,但也许仍然会有帮助。
是的,这个帖子我也看了,并安装上面的步骤做了,但是行不通,会出现“页面不存在”的错误。然后,我就迷茫了。
ok,我通过研究plugins/discourse-subscriptions的代码,大概实现了想要的效果。
访问/q/hello,显示自定义模板内容,并获取自定义的后端数据
下面的是我的步骤:
plugins/quectel/assets/javascripts/discourse/quectel-route-map.jsexport default function quectel() {
this.route("q/hello"); // 路由默认是/q/hello
};
plugins/quectel/assets/javascripts/discourse/routes/q/hello.jsimport Route from "@ember/routing/route";
import { ajax } from "discourse/lib/ajax";
export default class HelloRoute extends Route {
model() {
return ajax('/q/hello') // 请求后端接口,获取数据
}
}
plugins/quectel/assets/javascripts/discourse/templates/q/hello.gjs,渲染数据import QHello from '../../components/q-hello'
<template>
<h1>q-hello</h1>
{{!-- 渲染组件 --}}
<QHello />
{{!-- 渲染模型数据 --}}
<div>{{@model.hello}}</div>
</template>
plugins/quectel/app/controllers/quectel/examples_controller.rb# frozen_string_literal: true
module ::Quectel
class ExamplesController < ::ApplicationController
requires_plugin PLUGIN_NAME
def hello
render json: { hello: "hello 2333" }
end
end
end
plugins/quectel/config/routes.rb# frozen_string_literal: true
Quectel::Engine.routes.draw do
get "/hello" => "examples#hello"
end
Discourse::Application.routes.draw { mount ::Quectel::Engine, at: "q" }
访问/q/hello,将看到下面的显示: