我有类似register
的URL。如何获得return()
中React.js
内的import * as Knex from "knex";
import { Model } from "objection";
import { getKnexConfig } from "../../common/configuration";
import Category from "../models/category";
import Business from "../models/business";
const _knex = Knex.default(getKnexConfig());
Model.knex(_knex);
export async function up(knex: Knex, Promise): Promise<any> {
const BUSINESS_CACHE: Business[] = await Business.query();
return Promise.resolve()
.then(() =>
knex.schema.alterTable("Category", table => {
table.integer("parentCategoryId").unsigned();
table.string("primaryCountry");
})
)
.then(() => knex("Subcategory").select("*"))
.then(SUBCATEGORIES_CACHE => {
console.log(SUBCATEGORIES_CACHE);
let priority = 1;
const categoriesToSeed = SUBCATEGORIES_CACHE.map(category => {
const result = {
name: category.name,
imageUrl: category.imageUrl,
imageSmallUrl: category.imageSmallUrl,
iconUrl: category.iconUrl,
priority,
parentCategoryId: category.categoryId
};
priority++;
return result;
});
console.log("categoriesToSeed", categoriesToSeed);
return knex("Category").insert(categoriesToSeed);
})
.then(() =>
knex.schema.alterTable("Business", table => {
table.dropColumn("subcategoryId");
})
)
.then(() => knex.schema.renameTable("SubcategoryRisk", "CategoryRisk"))
.then(() =>
knex.schema.alterTable("CategoryRisk", table => {
table.renameColumn("subcategoryId", "categoryId");
})
)
.then(() =>
knex.schema.renameTable("SubcategoryMarketSize", "CategoryMarketSize")
)
.then(() =>
knex.schema.alterTable("CategoryMarketSize", table => {
table.renameColumn("subcategoryId", "categoryId");
})
)
.then(async () => {
const EPL_CATEGORY_ID = (await Category.query()
.where("name", "EPL")
.first()).id;
for (const business of BUSINESS_CACHE) {
await business.patch({ categoryId: EPL_CATEGORY_ID });
}
})
.then(() => knex.schema.dropTableIfExists("Subcategory"));
}
export async function down(knex: Knex): Promise<any> {
throw new Error("It's not possible to rollback this migration");
}
段?
答案 0 :(得分:1)
如下所示:
const url = "http://localhost:3000/register";
const segment = url.substring(url.lastIndexOf('/') + 1);
这将始终给出最后一段。假设您的网址是否为http://localhost:3000/user/John.
它将细分设为John
答案 1 :(得分:1)
在某些情况下也可以使用某些方法,如果您不总是想要最后一个细分。
如果您将URL作为字符串并要解析。 注意:这在IE11上不起作用,但是有诸如GitHub for URL polyfill 之类的polyfill(有关更多信息,请阅读MDN)
const url = new URL("http://localhost:3000/register");
const pathname = url.pathname; // contains "/register"
如果您尝试使用window对象访问URL,则几乎免费获得路径名:
const pathname = window.location.pathname; // also contains: "/register"